NHibernate查询覆盖投影、枚举和别名域转换器

NHibernate查询覆盖投影、枚举和别名域转换器,nhibernate,Nhibernate,如何使用QueryOver和AliasToBean将枚举值转换为字符串值?我有以下问题,但在尝试转换枚举时出错: SomeDTO someDTO = null; SomeReferenceAlias someReferenceAlias = null; var jobs = query .JoinAlias(x => x.SomeReference, () => someReferenceAlias, JoinT

如何使用QueryOver和AliasToBean将枚举值转换为字符串值?我有以下问题,但在尝试转换枚举时出错:

        SomeDTO someDTO = null;
        SomeReferenceAlias someReferenceAlias = null;
        var jobs = query
            .JoinAlias(x => x.SomeReference, () => someReferenceAlias, JoinType.InnerJoin)
            .SelectList(list => list
                .Select(p => p.SomeStatusEnum).WithAlias(() => someDTO.SomeStatus)//problem here
                .Select(p => someReferenceAlias.Name).WithAlias(() => someDTO.Name)
                )
            .TransformUsing(Transformers.AliasToBean<SomeDTO>())
            .Take(100)
            .List<SomeDTO>();
SomeDTO SomeDTO=null;
SomeReferenceAlias SomeReferenceAlias=null;
var jobs=query
.JoinAlias(x=>x.SomeReference,()=>someReferenceAlias,JoinType.InnerJoin)
.SelectList(list=>list
.Select(p=>p.SomeStatusEnum).WithAlias(()=>someDTO.SomeStatus)//此处有问题
.Select(p=>someReferenceAlias.Name).WithAlias(()=>someDTO.Name)
)
.TransformUsing(Transformers.AliasToBean())
.拿(100)
.List();

假设您的枚举在数据库中存储为int,我会尝试将字符串只读属性设置为自定义字符串类型:

public enum SomeStatus {up=1,right=2,down=3,left=4}


public class SomeStatusNhString : NHibernate.Type.AbstractStringType
{
    public SomeStatusNhString()
          : base(new StringSqlType())
       {
       }

    public SomeStatusNhString(StringSqlType sqlType)
          : base(sqlType)
       {
       }

    public override string Name
    {
        get { return "SomeStatusNhString"; }
    }

    public override object Get(System.Data.IDataReader rs, int index)
    {
        var x = base.Get(rs, index);
        return ((SomeStatus)int.Parse((string)x)).ToString();
    }
}
然后是你的地图

public virtual String StatusAsString{ get; set; }


<property name="StatusAsString" column="YOUR_COLUMN" not-null="true" insert="false" update="false" type="YourNameSpace.SomeStatusNhString, YourDll" access="property"></property>
公共虚拟字符串StatusAsString{get;set;}
希望这能有所帮助