【发布时间】:2019-11-07 12:43:46
【问题描述】:
我有一个返回 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]]'.
我做错了什么?
【问题讨论】:
-
做你的组不要匿名类型
.GroupBy(r => r.StartTime.Date )
标签: c# linq asp.net-core-mvc entity-framework-core asp.net-core-2.0