C# - Table - 将重复数据合并为一行(只申请一个字段)
C# - Table - Merging repetitive data into one line (applying for just one field)
如果这个问题已经得到回答,我很抱歉,但我正在尝试更改前端的实际 table 结果。
来自这个(与查询结果中的结构相同):
+----+--------+------------------+------------------+---------+
| id | name | in | out | country |
+----+--------+------------------+------------------+---------+
| 23 | Carlos | 23/03/2022 08:00 | 23/03/2022 17:00 | France |
+----+--------+------------------+------------------+---------+
| 23 | Carlos | 24/03/2022 08:10 | 24/03/2022 17:10 | France |
+----+--------+------------------+------------------+---------+
| 4 | Amanda | 23/03/2022 08:00 | 23/03/2022 17:00 | Italy |
+----+--------+------------------+------------------+---------+
对此:
+----+--------+------------------+------------------+---------+
| id | name | in | out | country |
+----+--------+------------------+------------------+---------+
| | | 23/03/2022 08:00 | 23/03/2022 17:00 | France |
| 23 | Carlos +------------------+------------------+---------+
| | | 24/03/2022 08:10 | 24/03/2022 17:10 | France |
+----+--------+------------------+------------------+---------+
| 4 | Amanda | 23/03/2022 08:00 | 23/03/2022 17:00 | Italy |
+----+--------+------------------+------------------+---------+
我尝试使用前端来做到这一点,但它合并了所有行,所以举个例子,carlos 将只显示在一行中,只获取每个字段的第一个元素,这是不正确的,因为省略值。
所以我猜这应该在查询中完成..但我不确定这对我有什么帮助
视图中的实际代码:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>In</th>
<th>Out</th>
<th>Country</th>
</tr>
</thead>
<tbody>
@foreach(var r in Model.Reports.OrderBy(x => x.Id))
{
<tr>
<td>@r.UserId</td>
<td>@r.Name</td>
<td>@r.In</td>
<td>@r.Out</td>
<td>@r.Country</td>
</tr>
}
</tbody>
</table>
您可以尝试统计重复数据的数量,然后使用rowspan,这里是一个工作演示:
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>In</th>
<th>Out</th>
<th>Country</th>
</tr>
</thead>
<tbody>
@{ var row = 1;}
@foreach (var r in Model.Reports.OrderBy(x => x.Id))
{
if (Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count > 1)
{
if (row == 1)
{
<tr>
<td rowspan=@Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count>@r.UserId</td>
<td rowspan=@Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count>@r.Name</td>
<td>@r.In</td>
<td>@r.Out</td>
<td>@r.Country</td>
</tr>
row++;
}
else
{
<tr>
<td>@r.In</td>
<td>@r.Out</td>
<td>@r.Country</td>
</tr>
if (row == Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count)
{
row = 1;
}
else {
row++;
}
}
}
else {
<tr>
<td>@r.UserId</td>
<td>@r.Name</td>
<td>@r.In</td>
<td>@r.Out</td>
<td>@r.Country</td>
</tr>
}
}
</tbody>
</table>
结果:
如果这个问题已经得到回答,我很抱歉,但我正在尝试更改前端的实际 table 结果。
来自这个(与查询结果中的结构相同):
+----+--------+------------------+------------------+---------+
| id | name | in | out | country |
+----+--------+------------------+------------------+---------+
| 23 | Carlos | 23/03/2022 08:00 | 23/03/2022 17:00 | France |
+----+--------+------------------+------------------+---------+
| 23 | Carlos | 24/03/2022 08:10 | 24/03/2022 17:10 | France |
+----+--------+------------------+------------------+---------+
| 4 | Amanda | 23/03/2022 08:00 | 23/03/2022 17:00 | Italy |
+----+--------+------------------+------------------+---------+
对此:
+----+--------+------------------+------------------+---------+
| id | name | in | out | country |
+----+--------+------------------+------------------+---------+
| | | 23/03/2022 08:00 | 23/03/2022 17:00 | France |
| 23 | Carlos +------------------+------------------+---------+
| | | 24/03/2022 08:10 | 24/03/2022 17:10 | France |
+----+--------+------------------+------------------+---------+
| 4 | Amanda | 23/03/2022 08:00 | 23/03/2022 17:00 | Italy |
+----+--------+------------------+------------------+---------+
我尝试使用前端来做到这一点,但它合并了所有行,所以举个例子,carlos 将只显示在一行中,只获取每个字段的第一个元素,这是不正确的,因为省略值。
所以我猜这应该在查询中完成..但我不确定这对我有什么帮助
视图中的实际代码:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>In</th>
<th>Out</th>
<th>Country</th>
</tr>
</thead>
<tbody>
@foreach(var r in Model.Reports.OrderBy(x => x.Id))
{
<tr>
<td>@r.UserId</td>
<td>@r.Name</td>
<td>@r.In</td>
<td>@r.Out</td>
<td>@r.Country</td>
</tr>
}
</tbody>
</table>
您可以尝试统计重复数据的数量,然后使用rowspan,这里是一个工作演示:
<table border="1">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>In</th>
<th>Out</th>
<th>Country</th>
</tr>
</thead>
<tbody>
@{ var row = 1;}
@foreach (var r in Model.Reports.OrderBy(x => x.Id))
{
if (Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count > 1)
{
if (row == 1)
{
<tr>
<td rowspan=@Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count>@r.UserId</td>
<td rowspan=@Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count>@r.Name</td>
<td>@r.In</td>
<td>@r.Out</td>
<td>@r.Country</td>
</tr>
row++;
}
else
{
<tr>
<td>@r.In</td>
<td>@r.Out</td>
<td>@r.Country</td>
</tr>
if (row == Model.Reports.Where(p => p.UserId == r.UserId).ToList().Count)
{
row = 1;
}
else {
row++;
}
}
}
else {
<tr>
<td>@r.UserId</td>
<td>@r.Name</td>
<td>@r.In</td>
<td>@r.Out</td>
<td>@r.Country</td>
</tr>
}
}
</tbody>
</table>
结果: