簡體   English   中英

在列表 ASP.NET Core 中添加具有相同日期的值

[英]Add values with same date in a List ASP.NET Core

我是asp.net核心的新手。 我有一個列表對象,其中包含大量數據,包括日期、訂單價值、交貨成本、訂單狀態等。

我想要做的是添加屬於同一日期的所有訂單值。 有人可以幫我解決這個問題嗎?

例如名單上的數據如下:-

Order Date                 Order Value
--------------------------------------
24/09/2020                     £3
24/09/2020                     £5
26/09/2020                     £7
26/09/2020                     £2
26/09/2020                     £3

我想不重復地顯示日期並添加具有相同日期的值並將其存儲在列表中

假設一個像這樣的Order類:

public class Order
{
    public DateTime DateCreated { get; set; }
    public decimal Value { get; set; }
}

您可以GroupBy訂單日期分組,然后再取每組訂單的Sum 下面是一個例子:

using System;
using System.Collections.Generic;
using System.Linq;

static void Main(string[] args)
{
    var orders = new List<Order>
    {
        new Order { DateCreated = DateTime.Parse("24/09/2020"), Value = 3 },
        new Order { DateCreated = DateTime.Parse("24/09/2020"), Value = 5 },
        new Order { DateCreated = DateTime.Parse("26/09/2020"), Value = 7 },
        new Order { DateCreated = DateTime.Parse("26/09/2020"), Value = 2 },
        new Order { DateCreated = DateTime.Parse("26/09/2020"), Value = 3 },
    };

    var orderTotals = orders
        .GroupBy(x => x.DateCreated)
        .Select(group => new 
        { 
            DateCreated = group.Key, 
            Total = group.Select(order => order.Value).Sum() 
        });

    foreach (var item in orderTotals)
    {
        Console.WriteLine($"{item.DateCreated} - {item.Total}");
    }
}

打印:

24/09/2020 00:00:00 - 8
26/09/2020 00:00:00 - 12

從評論更新

假設您使用的是實體框架,執行上述操作的過程幾乎相同。 為了在將數據返回給調用者方面更容易一些,讓我們定義另一個類:

public class OrderTotal
{
    public DateTime DateCreated { get; set; }
    public decimal Total { get; set; }
}

這意味着您可以在頁面中執行以下操作:

public class OrderModel : PageModel
{
    private readonly YourDbContext _dbContext;

    public OrderModel(YourDbContext dbContext)
    {
        _dbContext = dbContext;
    }
}

public IEnumerable<OrderTotal> OrderTotals { get; set; }

public async Task OnGetAsync()
{
    // Read the orders from the database, instead of from a list.
    var orderTotals = _dbContext.Orders
        .GroupBy(x => x.DateCreated)
        // Notice we're using OrderTotal now, rather than an
        // anonymous type. This is to make it easier to
        // use the data outside of this method.
        .Select(group => new OrderTotal
        { 
            DateCreated = group.Key, 
            Total = group.Select(order => order.Value).Sum() 
        });

    OrderTotals = await orderTotals.AsNoTracking().ToListAsync();
}

然后對於視圖部分:

@page
@model IEnumerable<YourProject.Models.OrderTotal>

<table>
    <thead>
      <tr>
        <th>Order Date</th>
        <th>Order Total</th>
      </tr>
    </thead>
    <tbody>
      @foreach (var order in Model)
      {
        <tr>
          <td>@order.DateCreated</td>
          <td>@order.Total</td>
        </tr>
      }
    </tbody>
</table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM