How do I transform an Enum value into a String Value using QueryOver and AliasToBean? 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 :
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.