我有一个简单的类,Status,它包含两个属性。我想为其中一个属性(状态ID)使用枚举,这样我就可以消除一堆魔术字符串。
我的问题是我如何使用它,例如:我有一个方法,它返回一个列表以在一个下拉框中进行绑定,如下所示-->
public static IList<Status> GetAdminStatuses()
{
IQueryable<Status> stat=context.tblAdminStatus
.Where(s => s.InactiveDate > DateTime.Now || s.InactiveDate == null)
.Select(s => new Status()
{
StatusID=s.StatusID,
StatusDescription=s.StatusDesc
});
return stat.ToList();
}
它显然不喜欢我的StatusID=s.StatusID
部分,因为数据库将它存储为 varchar。我是否在这里遗漏了一些简单的东西,或者我偶然发现了菜鸟领域并且不应该这样做?
这里的参考是类和枚举:
public class Status
{
public string StatusID {get; set;}
public string StatusDescription {get; set;}
}
public enum MyStatusID
{
draft, pending, declined, accepted, close
}
编辑
因此,在这里接受建议,我能够编译我的方法,但是在运行时我得到以下内容-->Method 'System.Object Parse(System.Type, System.String)' has no supported translation to SQL.
想法?
编辑 - 根据请求提供完整的方法,谢谢(注意 NoaStatusID == MyStatusID)
public static IList<Status> GetAdminStatuses(NoaStatusID currentStatus = NoaStatusID.draft)
{
using (var context = MemberDataContext.Create())
{
IQueryable<Status> stat=context.tblAdminStatus
.Where(s => s.InactiveDate > DateTime.Now || s.InactiveDate == null)
.Select(s => new Status()
{
StatusID=NoaStatusID)Enum.Parse(typeof(NoaStatusID),s.StatusID),
StatusDescription=s.StatusDesc
});
switch (currentStatus)
{
case NoaStatusID.draft:
stat=stat.Where(s => (s.StatusID == NoaStatusID.draft || s.StatusID == NoaStatusID.pending));
break;
case NoaStatusID.pending:
stat = stat.Where(s => (s.StatusID == NoaStatusID.accepted || s.StatusID ==NoaStatusID.declined || s.StatusID ==NoaStatusID.pending));
break;
case NoaStatusID.declined:
stat = stat.Where(s => (s.StatusID == NoaStatusID.draft || s.StatusID == NoaStatusID.pending || s.StatusID == NoaStatusID.declined));
break;
case NoaStatusID.accepted:
stat = stat.Where(s => (s.StatusID == NoaStatusID.mailed || s.StatusID == NoaStatusID.monitor || s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.accepted));
break;
case NoaStatusID.mailed:
stat = stat.Where(s => (s.StatusID == NoaStatusID.mailed || s.StatusID == NoaStatusID.monitor || s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.appeal));
break;
case NoaStatusID.monitor:
case NoaStatusID.appeal:
case NoaStatusID.close:
stat = stat.Where(s => (s.StatusID == NoaStatusID.close || s.StatusID == NoaStatusID.appeal));
break;
}
return stat.ToList();
}
}