var ps = dbContext.SplLedgers.Select(p => new SplLedgerModel
{
Name = p.Name,
VoucherType = Convert.ToString(((JIMS.VoucherTypes)p.VoucherType))
});
我收到以下异常,代码有什么问题。
JIMS.VoucherType是一个枚举
+ $exception {"LINQ to Entities does not recognize the method 'System.String ToString(System.Object)' method, and this method cannot be translated into a store expression."} System.Exception {System.NotSupportedException}
答案 0 :(得分:8)
您的代码基本上是在DB中找到Convert.ToString()方法,可以理解为失败。
您可以绕过它,例如确保查询在select之前执行,例如
var ps = dbContext.SplLedgers.Select(p => new
{
Name = p.Name,
VoucherType = p.VoucherType
}).ToList().Select(p => new SplLedgerModel(
{
Name = p.Name,
VoucherType = Convert.ToString(((JIMS.VoucherTypes)p.VoucherType))
});