如何使用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 :(得分:1)
假设你的枚举在数据库中存储为int,我会尝试将字符串readonly属性设置为自定义字符串类型:
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>
希望这可以提供帮助