如何计算 IEnumerable 中出现的次数
How Do I count the number of occurrences in the IEnumerable
我的 razor 页面中有这个 IEnumerable 方法。
@if (Orders.Any())
{
<table class="table table-bordered">
<thead>
<th>Driver Name</th>
<th>Delivery Orders</th>
</thead>
<tbody>
@foreach(var objOrders in Orders)
{
<tr>
<th>
@objOrders.Driver.UserName
</th>
<th>
@objOrders.Driver.FullName.Count()
</th>
<td>
</td>
</tr>
}
</tbody>
</table>
}
来自这里
private IEnumerable<OrderDTO> Orders { get; set; } = new List<OrderDTO>();
private bool IsLoading { get; set; } = true;
public async Task SearchDate()
{
Orders = await _orderRepository.GetDate(DateValue);
}
private async Task LoadOrders()
{
IsLoading=true;
StateHasChanged();
await SearchDate();
//Orders = await _orderRepository.GetDate(date);
IsLoading = false;
StateHasChanged();
}
这里
public async Task<IEnumerable<OrderDTO>> GetDate(DateTime date)
{
if (date != null)
{
return _mapper.Map<IEnumerable<Order>, IEnumerable<OrderDTO>>
((IEnumerable<Order>)_db.Orders.Where(u => u.DeliveryDate == date));
}
else
{
return _mapper.Map<IEnumerable<Order>, IEnumerable<OrderDTO>>(_db.Orders);
}
}
现在,显示是这样的
|Driver Name | Number Delivery Orders |
| :--- | :----: |
| 1 | 1 |
| 1 | 1 |
| 2 | 1 |
|Kim1 | 4 |
而我想这样显示
|Driver Name | Number Delivery Orders |
| :--- | :----: |
| 1 | 2 (Occurrences) |
| 2 | 1 (Occurrences) |
|Kim1 | 1 (Occurrences) |
我知道现在 Count()
方法正在计算 driver 名称的长度,但是我该如何编码才能使 return driver的名字。
谢谢大家。
@foreach(var objOrders in Orders
.GroupBy(u => u.Driver.FullName)
.Select(group => new {
Count= group.Count(),
Key=group.Key }
)
)
设法做到这一点。感谢@Martin Costello
我的 razor 页面中有这个 IEnumerable 方法。
@if (Orders.Any())
{
<table class="table table-bordered">
<thead>
<th>Driver Name</th>
<th>Delivery Orders</th>
</thead>
<tbody>
@foreach(var objOrders in Orders)
{
<tr>
<th>
@objOrders.Driver.UserName
</th>
<th>
@objOrders.Driver.FullName.Count()
</th>
<td>
</td>
</tr>
}
</tbody>
</table>
}
来自这里
private IEnumerable<OrderDTO> Orders { get; set; } = new List<OrderDTO>();
private bool IsLoading { get; set; } = true;
public async Task SearchDate()
{
Orders = await _orderRepository.GetDate(DateValue);
}
private async Task LoadOrders()
{
IsLoading=true;
StateHasChanged();
await SearchDate();
//Orders = await _orderRepository.GetDate(date);
IsLoading = false;
StateHasChanged();
}
这里
public async Task<IEnumerable<OrderDTO>> GetDate(DateTime date)
{
if (date != null)
{
return _mapper.Map<IEnumerable<Order>, IEnumerable<OrderDTO>>
((IEnumerable<Order>)_db.Orders.Where(u => u.DeliveryDate == date));
}
else
{
return _mapper.Map<IEnumerable<Order>, IEnumerable<OrderDTO>>(_db.Orders);
}
}
现在,显示是这样的
|Driver Name | Number Delivery Orders |
| :--- | :----: |
| 1 | 1 |
| 1 | 1 |
| 2 | 1 |
|Kim1 | 4 |
而我想这样显示
|Driver Name | Number Delivery Orders |
| :--- | :----: |
| 1 | 2 (Occurrences) |
| 2 | 1 (Occurrences) |
|Kim1 | 1 (Occurrences) |
我知道现在 Count()
方法正在计算 driver 名称的长度,但是我该如何编码才能使 return driver的名字。
谢谢大家。
@foreach(var objOrders in Orders
.GroupBy(u => u.Driver.FullName)
.Select(group => new {
Count= group.Count(),
Key=group.Key }
)
)
设法做到这一点。感谢@Martin Costello