我有一个LINQ语句,我想要做一个类型为String的单个列的总和.
我正在尝试执行以下语句,其中我是Converting.ToInt32.当我运行这个时,我收到以下错误.
LINQ to Entities无法识别方法'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)
)
};
Run Code Online (Sandbox Code Playgroud)
我已经尝试了许多不同的数据类型来转换并得到类似的错误.
您无法翻译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));
Run Code Online (Sandbox Code Playgroud)
更新:在where子句后移动ToList().