我有枚举:
public enum CmdType { [Display(Name = "abc")] AbcEnumIdentifier = 0, [Display(Name = "xyz")] XyzEnumIdentifier = 1, ... }
我想将每个枚举的名称放入我的查询中,但即使使用.WithTranslations()
我也会收到此错误:
LINQ to Entities无法识别方法'System.String GetName(System.Type,System.Object)'方法,并且此方法无法转换为商店表达式.
查询:
var joinedRecord = ( from m in mTable join b in bTable on m.Id equals b.aRefId select new { aId = a.Id, aAttrib1 = a.Attrib1 ... bCmdType = Enum.GetName(typeof(CmdType), b.CmdType) } ).WithTranslations();
如何Enum.GetName(...)
在查询中返回生成的值?
LINQ to实体尝试将您的查询转换为SQL,但它无法执行此操作,因为Enum.GetName
SQL中没有等效的方法.
您需要具体化查询结果并将枚举值转换为内存中的名称.
var joinedRecords = ( from m in mTable join b in bTable on m.Id equals b.aRefId select new { aId = a.Id, aAttrib1 = a.Attrib1 ... bCmdType = b.CmdType } ).AsEnumerable() //Executes the query, further you have simple CLR objects .Select(o => new { aId = o.Id, aAttrib1 = o.Attrib1 ... bCmdTypeName = Enum.GetName(typeof(CmdType), o.CmdType) });