2012-05-05 22 views
4

如何使用QueryOver和AliasToBean将枚举值转换为字符串值?我有以下的,但得到一个错误试图改造枚举时:NHibernate 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>(); 
+0

在这个任何更新? – dotjoe

回答

1

假设你的枚举存储在你的数据库的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> 

希望这可以帮助