简体   繁体   English

如何对强类型模型进行分组

[英]How to group a strongly typed model

I pass my model to my view. 我将模型传递给我的观点。 On this view I am displaying a table and trying to populate it with a list of information from the database. 在此视图上,我正在显示一个表,并尝试用数据库中的信息列表填充该表。

I am using asp.net-mvc, entity framework, razor, c# and LINQ 我正在使用asp.net-mvc,实体框架,剃刀,C#和LINQ

However I need it grouped by a certain column kpiID . 但是我需要将它按某个列kpiID分组。

Here are all of the columns in the table. 这是表中的所有列。

kpiHistoryID
kpiID
startAmount
completedamount
endAmount
completeBy
comments

The kpiID links back the the KPI table and is where I can grab the actual name of the KPI. kpiID链接回KPI表,在这里我可以获取KPI的实际名称。

How exactly do I group the data and can it be done in the below @foreach line? 我如何精确地对数据进行分组,并可以在下面的@foreach行中完成?

 <tbody>
    @foreach (var item in @Model.IPACS_Department.IPACS_Functions.SelectMany(m => m.IPACS_Processes).SelectMany(m => m.IPACS_Procedures).SelectMany(m => m.IPACS_KPIS).SelectMany(m => m.IPACS_kpiHistory))
    {
        <tr class="gradeX">
            <td>
                @item.IPACS_KPIS.name
            </td>
            <td>
                @item.startAmount
            </td>
            <td>
                @item.completedAmount
            </td>
            <td>
                @item.endAmount
            </td>
        </tr>
    }
</tbody>

This: 这个:

@Model.IPACS_Department.IPACS_Functions.SelectMany(m => m.IPACS_Processes)
.SelectMany(m => m.IPACS_Procedures).SelectMany(m => m.IPACS_KPIS)
.SelectMany(m => m.IPACS_kpiHistory)

gives a list of denormalised kpiHistories, yes? 给出非规范化的kpiHistories列表,是吗? Does it not give you the answer you want if you do 如果您这样做,它不会给您想要的答案吗

thatList.GroupBy(x => x.kpiID)

Only way I could figure out how to do it was using sum. 我只能找出总和的方法。 Couldn't get it to work with group by at all. 根本无法与group by一起使用。

<tbody>
    @foreach (var item in @Model.IPACS_Department.IPACS_Functions.SelectMany(m => m.IPACS_Processes).SelectMany(m => m.IPACS_Procedures).SelectMany(m => m.IPACS_KPIS))
    {
        <tr class="gradeX">
            <td>
                @item.name
            </td>
            <td>
                @item.IPACS_kpiHistory.Select(m => m.startAmount).Sum()
            </td>
            <td>
                @item.IPACS_kpiHistory.Select(m => m.completedAmount).Sum()
            </td>
            <td>
                @item.IPACS_kpiHistory.Select(m => m.endAmount).Sum()
            </td>
        </tr>
    }
</tbody>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM