【问题标题】:How to iterate over grouped LINQ data in MVC view如何在 MVC 视图中迭代分组的 LINQ 数据
【发布时间】:2012-12-03 17:36:06
【问题描述】:

我有两张桌子。一个名为 Occurrence 的表包含以下内容:

OccurrenceID | EmployeeID | OccurrenceDate | Points | Comment
-------------------------------------------------------------
1            |1           |2012-01-01      |5       |yada    
2            |1           |2012-02-01      |3       |blah    
3            |2           |2012-03-01      |2       |yada

另一个表名为 Employee,包含以下内容:

EmployeeID | EmployeeName
-------------------------
 1         |Jack
 2         |Jill

我正在尝试将这两个表组合在一起,并最终为每个员工生成一行,该行将在我的 MVC 4 项目的视图中显示总分。所以对于上面的例子,我的输出应该是:

Name    | Points
----------------
Jack    |8
Jill    |2

这是我在控制器中尝试过的 LINQ 查询:

        var groupedOccurrences = from o in db.Occurrences.Include(o => o.Employee)
                                 where o.OccurrenceDate >= beginDate
                                    && o.OccurrenceDate <= endDate
                                 group o by new {o.EmployeeID, o.Points} into g
                                 select new {
                                     Name = g.Key.EmployeeID,
                                     Total = g.Sum(o => o.Points)
                                 };

        return View(groupedOccurrences);

这是我的观点:

@model IEnumerable<PtoTracker.Occurrence>

<table>
    <tr>
        <th>
            Employee
        </th>
        <th>
            Total Pts.
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Employee.EmployeeName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Points)
        </td>
    </tr>
}
</table>

当我导航到此视图时,我收到此错误:

传入字典的模型项的类型为“System.Data.Entity.Infrastructure.DbQuery1[&lt;&gt;f__AnonymousType32[System.Int32,System.Int32]]”,但此字典需要“System”类型的模型项.Collections.Generic.IEnumerable`1[PtoTracker.Occurrence]'。

有人可以帮助我了解我应该做些什么不同的事情吗?

【问题讨论】:

    标签: c# asp.net-mvc linq


    【解决方案1】:

    您需要新建视图预期类的实例

    var groupedOccurrences = 
       (from o in db.Occurrences.Include(o => o.Employee)
        where o.OccurrenceDate >= beginDate && o.OccurrenceDate <= endDate
        group o by new {o.EmployeeID, o.Points} into g
        select new { Name = g.Key.EmployeeID, Total = g.Sum(o => o.Points)}
       ).AsEnumerable();
    
    
    var model = groupedOccurrences.Select(item => new PtoTracker.Occurrence { 
                             Name = item.Name,
                             Total = item.Total });
    

    您的视图期望 IEnumerablePtoTracker.Occurrence,但您发送的却是匿名类型的 IQuerable

    【讨论】:

    • 这给了我一个错误,说 无法在 LINQ to Entities 查询中构造实体或复杂类型“PtoTrackerModel.Occurrence”。
    • @Splendor 使用您在 OP 中的查询,然后在它之后添加 AsEnumerable 以便在 Linq-to-Objects 中评估查询的其余部分,然后添加映射的 Select Occurrence 对象的匿名对象。
    • @Servy 你能帮我理解如何添加一个将匿名对象映射到我的Occurrence 对象的Select 吗?这是我的第一个 MVC 项目,我不确定这究竟意味着什么。
    • @Splendor .Select(item =&gt; new Occurrence(){ ... });
    猜你喜欢
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    相关资源
    最近更新 更多