c# - Summing by multiple columns in a list
问题描述
I am creating a html table to display values in a List<> the list has three columns: the name of the indicator, the year and month in which it was selected and the count of the number of times it has been filtered on.
the code and html I am using is shown below:
<table class="users">
<tr>
<th></th>
@foreach (var monthYear in Model.KeyIndicatorUseCount.Select(m => m.MonthYear).Distinct())
{
<th>@monthYear</th>
}
</tr>
@foreach (var keyIndicator in Model.KeyIndicatorUseCount.Select(m => m.Name).Distinct())
{
<tr>
<td>@keyIndicator</td>
@foreach (var monthYear in Model.KeyIndicatorUseCount.Select(m => m.MonthYear).Distinct())
{
<td>@Model.KeyIndicatorUseCount.Sum(m => m.Count)</td>
}
</tr>
}
</table>
Currently the table is being populated with the sum of the values for the first month-year column.
The calculation needs to be the sum of the number of views for each key indicator with that month-year.
For reference here is a screenshot of what is currently being produced:
How do I go about performing the calculation to get a sum which corresponds to each month-year for the key indicator name in the first column?
解决方案
@foreach (var keyIndicator in item.OrderBy(f => f.Name).GroupBy(f => f.Name))
{
<tr>
<td>@keyIndicator.Key</td>
@foreach (var monthYear in keyIndicator.OrderBy(f => f.MonthYear).GroupBy(f => f.MonthYear))
{
<td>@(keyIndicator.Sum(f => f.Count))</td>
}
<td>
@(keyIndicator.Sum(f => f.Count)) /* if you want to sum count of every month of year */
</td>
</tr>
}
推荐阅读
- python - Keras 多类分类过程耗时较长
- javascript - JS:切换/案例更改类名不起作用
- spring - 浏览器中的 HTTP 响应标头为空,但在 POSTMAN 中填写
- jasper-reports - 如何在碧玉报告中为组设置序列号
- python - Django 表单动作
- java - 如何将 Eclipse(消息)控制台的输出保存到文件中?
- angular - Ionic 中的 Firebase 推送通知
- laravel - laravel 5验证自定义错误消息不出现
- android - 如果未授予权限,则不会触发 onShowFileChooser
- c++ - C++中没有模板的模板类的decltype