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