本文介绍了LINQ to Entities无法识别方法'Int32 ToInt32将字符串转换为Int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ语句,我想对某个类型为String的单列求和.

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

我正在尝试在Converting.ToInt32中执行以下语句.当我运行此程序时,出现以下错误.

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

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

我也尝试了多种不同的数据类型来进行转换并得到类似的错误.

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

推荐答案

您不能将ToInt32转换为T-SQL.一种使它起作用的方法是根据这样从数据库中检索到的列表在内存中运行它

You can't translate ToInt32 to T-SQL. One way to make it work is to run it in memory based on the list retrieved from the database like this

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));

已更新:将ToList()移到where子句之后.

Updated: Moved the ToList() after the where clause.

这篇关于LINQ to Entities无法识别方法'Int32 ToInt32将字符串转换为Int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 08:27