【发布时间】:2018-08-17 02:31:05
【问题描述】:
以下代码失败:
Iqueryable<shift> list = null;
list = context.shifts.Where(o => o.create_date == date).GroupBy(m => m.empId).select(
g=>
new{
empId = g.Key,
time = g.Sum(s => s.hours) // need to calculate sum of hours worked in each employee
}
);
导致报如下错误:
Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.shift'
【问题讨论】:
-
使用
var而不是IQueryable<shift>。对于匿名类型的实例,您的表达式返回IEnumerable,而不是shift值。对匿名类型的引用必须声明为var。 -
首先,这段代码有大小写问题,会导致编译失败。请发布确切的代码。其次,您选择匿名类型,但随后将其设置为
IQueryable<shift>,这将失败,但出现不同的错误,因此再次发布实际代码。 -
您发布的错误信息不正确。您至少应该确保您向我们提供准确的信息。出于这个原因,我投票结束这个问题。
标签: c# asp.net-mvc linq asp.net-web-api