2013-09-05 188 views
1

我有一个LINQ语句,我想对字符串类型的单个列进行求和。LINQ to Entities无法识别方法'Int32 ToInt32将字符串转换为Int

我想要做以下的语句,我是Converting.ToInt32。当我运行这个我得到以下错误。

LINQ实体无法识别方法“的Int32 ToInt32(System.String)”方法,和这种方法不能被翻译成 商店表达。

LINQ声明

var CoreData = new 
    { 
    Shoots = 
    (from item in coresdb.Productions 
    where item.Date >= StartShift && item.Date <= EndDate 
    select item).Sum(x => Convert.ToInt32(x.ShootCount) 
    ) 
}; 

我已经尝试了许多不同类型的数据过于转换,并得到了类似的错误。

+0

Duplicate:http://stackoverflow.com/questions/13887296/linq-to-entities-does-not-recognize-the-method-int32-int32system-string-meth – melancia

回答

5

您不能将ToInt32转换为T-SQL。使它工作的一种方法是基于从数据库检索这样

var CoreData = coresdb.Productions 
    .Where(item => item.Date >= StartShift && item.Date <= EndDate) 
    .ToList() // get the data into memory first. 
    .Sum(x => Convert.ToInt32(x.ShootCount)); 

更新名单上的内存来运行它:where子句后移动ToList()。

0

如果你不想实现查询(检索数据),你可以使用cast(即(int)x.ShootCount)。被转换为SQL CAST AS语句。

相关问题