1

I have a LINQ statement that i want to do a sum of some a single column that is of type String.

I am trying to do the following statement where i am Converting.ToInt32. When i run this i am getting the following error.

LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression.

LINQ statement

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

I have tried a number of different data types to convert too and get a similar error.

4

2 回答 2

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()。

于 2013-09-05T13:50:00.840 回答
0

如果您不想具体化查询(检索数据),您可以使用强制转换(即(int)x.ShootCount)。被转换为 SQL CAST AS 语句。

于 2015-06-24T09:35:47.750 回答