4

如何使用 QueryOver 和 AliasToBean 将枚举值转换为字符串值?我有以下内容,但在尝试转换 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>();
4

1 回答 1

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>

希望这可以帮助

于 2012-05-07T16:30:37.397 回答