0
我想根据条件将lambda传递给我的.Select()方法。将lambda作为变量传递给.Select()
设置我的拉姆达像这样:
Func<Monthly, int?> f = x => x.CLDD;
然后我建立了我。选择()这样的:
IQueryable query =
db.Monthlies
.GroupBy(o => o.Date.Value.Year)
.Select(
o => new {
Year = o.Key,
MaxDate = o.Max(x => x.Date),
Data = o.Sum(f)
}
)
.Where(o => o.Year != currentYear)
.OrderBy(o => o.Year);
代码编译并运行正常,但查询不发返回任何结果。当我调试和手表查询我看到它说:如果
+ base {"Internal .NET Framework Data Provider error 1025."}
System.SystemException {System.InvalidOperationException}
注意,而不是我做的:
Expression<Func<Monthly, int?>> f = x => x.CLDD;
然后o.Sum(F)的错误说:
Error 1 Instance argument: cannot convert from
'System.Linq.IGrouping<int,MyWeb.Models.Monthly>' to
System.Linq.IQueryable<MyWeb.Models.Monthly>'
谢谢!
谢谢Equiso - 这真是棒极了! – mbird
@Equiso:请将您的答案作为实际答案发布,以便mbird可以接受它。否则,这个问题看起来还没有得到回答,浪费了其他人的时间,并阻止人们在需要时找到答案。 (mbird:如果Equiso最终没有发布实际答案,请注意,您可以自己做,然后再接受它,以确保此问题被正确显示为已回答)。 –
[相关的评论回答的meta post](http://meta.stackoverflow.com/questions/251597/question-with-no-answers-but-issue-solved-in-the-comments) – ryanyuyu