在 Nhibernate QueryOver 中使用计算属性(未映射)

emr*_*mre 5 c# sql linq nhibernate

我有一个表 A,它有以下列:

AId - TargetId - IsActive

对应于这个表,我有以下类(带有额外的计算属性)和映射器:

public class A 
{
    public virtual long AId { get; set; }
    public virtual int TargetId { get; set; }
    public virtual int IsActive { get; set; }

    //calculated property, doesn't exist in the table
    public virtual bool IsClientSide
    {
        get { return ((this.TargetId & TargetEnum.ClientSide) != 0); }
    }
}
Run Code Online (Sandbox Code Playgroud)
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Type;
using ANamespace.A;

namespace mapping 
{
    public class AMap : ClassMapping<A> 
    {
        public AMap() 
        {
            this.Cache(x => { x.Usage(CacheUsage.NonstrictReadWrite); x.Region("LongTerm"); x.Include(CacheInclude.All); });
            this.Lazy(false);
            this.Mutable(true);
            this.DynamicUpdate(true);
            this.Id(x => x.AId, map => map.Generator(Generators.Native));
            this.Property(x => x.TargetId, map => { map.NotNullable(true); map.Type<EnumType<TargetEnum>>(); });
            this.Property(x => x.IsActive, map => map.NotNullable(true));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我没有映射这个 IsClientSide 属性,因为它不在表中。但我想在我的查询中使用它,如下所示:

A aobject = null;
alist = session.QueryOver(() => aobject)

        .Where(a => a.IsClientSide)
        .And(tt => a.IsActive)
        ......
Run Code Online (Sandbox Code Playgroud)

我找到了 Hendry Luk 的“Linq-ing 计算属性”帖子,但 ILinqToHqlGenerator 使用这个小属性似乎过于复杂。

我该如何IsClientSide在我的映射器类中制定属性?

Rad*_*ler 5

我们需要 1)Formula映射和 2) bitwise运算符

Property(x => x.IsClientSide, map =>
{
    map.Formula("(Target_ID & 1 <> 0)");
});
Run Code Online (Sandbox Code Playgroud)

并且属性应该可以由 NHibernate 分配

public virtual bool IsClientSide { get; protected set; }
Run Code Online (Sandbox Code Playgroud)

现在我们可以IsClientSide在任何查询中使用...