[英]Linq GroupBy expression raises AnonymousType exception
我有一個查詢,該查詢返回一個IEnumerable
,用於顯示值表。
我想通過模型的DateTime
字段( StartTime
)將表分組(使用<H3/>
元素)。
我更改了模型以使用適當的泛型:
IEnumerable<IGrouping<DateTime, FooViewModel>> routes;
我通過FromSQL
查詢填充模型:
routes = (IEnumerable<IGrouping<DateTime, FooViewModel>>)await _context.FooViewModels
.FromSql(@"SELECT ..."
)
.GroupBy(r => new { r.StartTime.Date })
.ToListAsync();
當代碼通過設計時要求時,在運行時出現錯誤:
Unable to cast object of type 'System.Collections.Generic.List`1[System.Linq.IGrouping`2[<>f__AnonymousType5`1[System.DateTime],FooViewModel]]' to type 'System.Collections.Generic.IEnumerable`1[System.Linq.IGrouping`2[System.DateTime,FooViewModel]]'.
我做錯了什么?
由於錯誤正試圖告訴您,您無法將匿名類型作為DateTime
傳遞。
由於您的視圖接受DateTime
的分組,因此需要將DateTime
lambda傳遞給GroupBy()
,以使其具有相同的類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.