C#GroupBy遇到麻烦的两个关键

时间:2019-08-06 18:56:21

标签: c# asp.net-mvc group-by

在我的控制器中,我想按两列对数据库表记录进行分组。

到目前为止,这是我的代码:

List<tblFAM> tblFAMs = db.tblFAMS.Where(x => x.CDate.Year == year && x.Ftype == 1).ToList();

vmMonthlyReport.FByCounty = tblFAMs.GroupBy(x => new { x.code_County.County, x.CDate.Month})
    .Select(t => new FByCountyProxy()
    {
        CountyName = t.Key.County,
        MonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(t.Key.Month),
        CountyCount = t.Count()
    }).ToList();

这在我看来是一个问题:

enter image description here

这是我的查看代码:

<table class="table table-bordered table-hover table-responsive">
    <thead>
    <tr>
        <th></th>
        @foreach (var month in Model.FByCounty)
        {
            <th>
                @month.MonthName
            </th>
        }
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model.FByCounty)
    {
        <tr>
            <td>@item.CountyName</td>
            <td>@item.CountyCount</td>
        </tr>
    }
    </tbody>
</table>

我在寻找什么

|          | Jan |  Feb  | Mar | Apr | May | Jun | Jul |
---------------------------------------------------------
New Castle |  x  |   x   |  x  |  x  |  x  |  x  |  x  |
Kent       |  x  |   x   |  x  |  x  |  x  |  x  |  x  |
Sussex     |  x  |   x   |  x  |  x  |  x  |  x  |  x  |

我想要的是使月份名称只在顶部显示一次(不重复,因为您可以看到的情况离图片很远)...然后表的主体中只有3行。 。每个县一个。.然后在相应月份的每个td元素中计数。

如果我按这两列进行分组,我不确定为什么要重复月份和县?另外,如果特定县的Count0 ..我也希望显示它,而不仅仅是空白。

更新

C#-控制器

List<tblFAM> tblFAMs = db.tblFAMS.Where(x => x.CDate.Year == year && x.Ftype == 1).ToList();

            vmMonthlyReport.FByCounty = tblFAMs.GroupBy(x => x.code_County.County).ToList();

C#-类

public class vmMonthlyReport
{
    public vmMonthlyReport()
    {
        FByCounty = new List<IGrouping<string, tblFAM>>();();
    }
    public List<IGrouping<string, tblFAM>> FByCounty { get; set; }
}

查看

<table class="table table-bordered table-hover table-responsive">
    <thead>
        <tr>
            <th></th>
            @for (var i = 1; i <= 12; i++)
            {
                <th>
                    @CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(i)
                </th>
            }
            <th>Total</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.FByCounty)
        {
            <tr>
                <td>@item.Key</td>
                @for (var i = 1; i <= 12; i++)
                {
                    <td>
                        @item.Count(x => x.CDate.Month == i)
                    </td>
                }
                @*<td>
                    @Model.FByCounty.Count(x => x.Key == item.Key)
                </td>*@
            </tr>
        }
    </tbody>
</table>

1 个答案:

答案 0 :(得分:2)

首先创建月份名称只需执行以下操作

@foreach (int i = 1; i <= 12; i++)
{
    <th>
        @CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(i)
    </th>
}

这样,即使您没有所有月份的数据,您也应该得到所有月份。

然后填充它,您可以执行以下操作

<tbody>
@foreach (var item in Model.FByCounty.GroupBy(x => x.CountyName))
{
    <tr>
        <td>@item.Key</td>
        @for(int i = 1; i <= 12; i++)
        {
            <td>@item.Where(x => x.MonthName == CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(i))
                 .Select(x => x.CountyCount)
                 .SingleOrDefault()</td>
        }
    </tr>
}
</tbody>

尽管最好选择月份号而不是名称,以简化代码。