当我尝试将 Linq 查询的结果传递给 MVC.Webgrid 时,出现错误(简化):
The model item passed into the dictionary is of
type'System.Data.Entity.Infrastructure.DbQuery but
this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[MyModel.Models.MyMetric]'
这是失败的 ActionResult:
public ActionResult Test()
{
var metrics = db.Metrics
.GroupBy(c => c.Year)
.Select(g => new
{
Year = g.Key,
Q1 = g.Where(c => c.Quarter == 1).Sum(c => c.Value),
Q2 = g.Where(c => c.Quarter == 2).Sum(c => c.Value),
Q3 = g.Where(c => c.Quarter == 3).Sum(c => c.Value),
Q4 = g.Where(c => c.Quarter == 4).Sum(c => c.Value)
});
return View(metrics);
}
我假设因为我正在创建原始数据的一个支点,所以我需要创建某种新模型,但我不确定如何在这种情况下做到这一点。(只是用 EF 和 MVC 弄湿了我的脚)
有任何想法吗?
蒂亚杰