LINQ to Entities无法识别该方法,并且此方法无法转换为存储表达式

时间:2012-09-28 14:38:42

标签: c# .net wpf entity-framework linq-to-sql

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}

1 个答案:

答案 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)) 
    });