[英]NHibernate QueryOver projections, enums and aliastobean transformers
How do I transform an Enum value into a String Value using QueryOver and AliasToBean? 如何使用QueryOver和AliasToBean将Enum值转换为字符串值? I have the following but get an error when trying to transform the Enum: 我有以下但尝试转换枚举时出错:
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>();
Assuming your enum is stored as int in your DB, I would try a string readonly property to a custom string type : 假设你的枚举在数据库中存储为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();
}
}
And then your mapping 然后你的映射
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>
Hope this can help 希望这可以提供帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.