2012-09-28 27 views
3
var ps = dbContext.SplLedgers.Select(p => new SplLedgerModel 
      { 
       Name = p.Name, 
       VoucherType = Convert.ToString(((JIMS.VoucherTypes)p.VoucherType)) 
     }); 

我收到以下异常,代码有什么问题。LINQ to Entities无法识别该方法,并且此方法无法转换为商店表达式

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

好链接讨论这个问题:http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/19a33909-c532-419b-a533-57e431c0b30b – dugas

回答

5

您的代码基本上是试图发现在DB的Convert.ToString()方法和理解失败。

您可以避开它,例如通过确保查询在选择之前执行,例如,

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)) 
    }); 
+1

具有检索所有的缺点SplLedgers的列,而不仅仅是Name和VoucherType(这可能会或可能不重要)。 – sgmoore

+0

公平点,所以可以像上面那样精致 –

+0

但是,当我试图使用CollectionViewSource将此数据绑定到我的数据网格...有行但内容不可见。 –

相关问题