【发布时间】:2014-07-11 01:05:00
【问题描述】:
我有一个名为Comment 的实体,它有一个enum 类型为CommentType 的属性:
public class Comment
{
public virtual Guid Id { get; private set; }
public virtual CommentType CommentType { get; set; }
// other prop removed for simplicity
}
public enum CommentType
{
Comment,
Correction,
Improvement,
BugFix,
NewFeauture,
Other
}
我需要通过CommentType 枚举的alphabetically 值从数据库中选择 cmets,类似于
_db.Comments.OrderBy(p => p.CommentType)
但是,Enum 值被视为整数,因此无法按字母顺序正确排序。
有什么方法可以将一些属性/元数据添加到 Enum 值以使它们按字母顺序正确排序?
一种解决方案是将整数值分配给枚举值,但我已经有许多需要更新的数据库记录。而且这种解决方案不适合新添加的枚举值。
public enum CommentType
{
Comment = 2,
Correction = 3,
Improvement = 4,
BugFix = 1,
NewFeauture = 5,
Other = 6
}
【问题讨论】:
-
尝试使用
p => p.CommentType.ToString() -
@DoanCuong,要完成这项工作,您必须首先调用 _db.Comments.ToList(),就像下面提到的 Machet。否则会导致运行时错误。
标签: asp.net entity-framework sorting enums linq-to-entities