C#LINQ分组并求和List <T>,然后分组并求和第一个List中的另一个列表

时间:2019-06-07 19:25:44

标签: c# linq

试图将一个列表的结果(数据)分组到另一个要显示的列表中。 LINQ查询需要什么才能获得我想要的结果。

我需要的第一个分组是ProductId,然后在结果列表中,我需要再次按Sitename分组,我也想对这些分组中的总数求和。

-课程

public class Item
{
    public string PartNumber { get; set; }
    public long ProductId { get; set; }
    public long TotalSold { get; set; }
    public long TotalOrders { get; set; }
    public long TotalAvailable { get; set; }
    public DateTime LastSold { get; set; }

    public List<MarketPlace> MarketPlaceList = new List<MarketPlace>();

}

public class MarketPlace
{
    public string SiteName { get; set; }
    public long SiteTotalQuantity { get; set; }
    public long SiteTotalOrders { get; set; }
}

-c#代码

var rolledUpList =    from e in itemList
                      group e by e.ProductId
                      into newGroup
                      orderby newGroup.Key
                      select newGroup;

foreach (var nameGroup in rolledUpList)
{
    Console.WriteLine($"Key: {nameGroup.Key}");
    foreach (var item in nameGroup)
    {

        Console.WriteLine($"\t{item.ProductId}, {item.PartNumber}, Total Orders({item.TotalOrders}) - Total Quantity ({item.TotalSold})");

        foreach (var m in item.MarketPlaceList)
        {
            Console.WriteLine($"\t\t{m.SiteName}, Total Orders({m.SiteTotalOrders}) - Total Quantity ({m.SiteTotalQuantity})");
        }
    }
}

---产生的输出结果

Key: 42804

42804, L89509-WFL-EA, Total Orders(1) - Total Quantity (1)
 {Amazon Seller Central, Total Orders(1) - Total Quantity (1)}
42804, L89509-WFL-EA, Total Orders(1) - Total Quantity (1)
 {Walmart Marketplace, Total Orders(1) - Total Quantity (1)}
42804, L89509-WFL-EA, Total Orders(1) - Total Quantity (1)
 {Walmart Marketplace, Total Orders(1) - Total Quantity (1)}

Key: 42812

42812, L89501-WFL-EA, Total Orders(1) - Total Quantity (1)
 {BigCommerce, Total Orders(1) - Total Quantity (1)}
42812, L89501-WFL-EA, Total Orders(1) - Total Quantity (1)
 {Amazon Seller Central, Total Orders(1) - Total Quantity (1)}
42812, L89501-WFL-EA, Total Orders(1) - Total Quantity (1)
 {Walmart Marketplace, Total Orders(1) - Total Quantity (1)}
42812, L89501-WFL-EA, Total Orders(1) - Total Quantity (1)
 {Walmart Marketplace, Total Orders(1) - Total Quantity (1)}
42812, L89501-WFL-EA, Total Orders(1) - Total Quantity (1)
 {Walmart Marketplace, Total Orders(1) - Total Quantity (1)}

Key: 42943

42943, L80421-W-EA, Total Orders(1) - Total Quantity (1)
 {eBay, Total Orders(1) - Total Quantity (1)}
42943, L80421-W-EA, Total Orders(1) - Total Quantity (1)
 {eBay, Total Orders(1) - Total Quantity (1)}

Key: 43838

43838, L41080-6WP-EA, Total Orders(1) - Total Quantity (1)
 {Amazon Seller Central, Total Orders(1) - Total Quantity (1)}
43838, L41080-6WP-EA, Total Orders(1) - Total Quantity (14)
 {BigCommerce, Total Orders(1) - Total Quantity (14)}

-需要或期望的

Key: 42804

 42804, L89509-WFL-EA, Total Orders(3) - Total Quantity (3)
   {Amazon Seller Central, Total Orders(1) - Total Quantity (1)}
   {Walmart Marketplace, Total Orders(2) - Total Quantity (2)}  

Key: 42812

 42812, L89501-WFL-EA, Total Orders(5) - Total Quantity (5)
   {BigCommerce, Total Orders(1) - Total Quantity (1)}
   {Amazon Seller Central, Total Orders(1) - Total Quantity (1)}
   {Walmart Marketplace, Total Orders(3) - Total Quantity (3)}

Key: 42943

 42943, L80421-W-EA, Total Orders(2) - Total Quantity (2)
   {eBay, Total Orders(2) - Total Quantity (2)}

Key: 43838

 43838, L41080-6WP-EA, Total Orders(2) - Total Quantity (15)
  {Amazon Seller Central, Total Orders(1) - Total Quantity (1)}
  {BigCommerce, Total Orders(1) - Total Quantity (14)}

1 个答案:

答案 0 :(得分:2)

您的答案需要嵌套的group...by-外部分组,然后展平MarketPlaceList并按SiteName分组:

var ans = from i in itemList
          group i by i.ProductId into ig
          let PartNumber = ig.First().PartNumber
          select new {
              ProductId = ig.Key,
              PartNumber,
              TotalOrders = ig.Sum(i => i.TotalOrders),
              TotalSold = ig.Sum(i => i.TotalSold),
              MarketPlaceList = (from m in ig.SelectMany(i => i.MarketPlaceList)
                                 group m by m.SiteName into mg
                                 select new {
                                     SiteName = mg.Key,
                                     SiteTotalOrders = mg.Sum(m => m.SiteTotalOrders),
                                     SiteTotalQuantity = mg.Sum(m => m.SiteTotalQuantity)
                                 }).ToList()
          };

我使用了匿名类型,但是由于属性匹配,您当然可以使用现有的类。

然后通过以下方式生成所需的输出:

foreach (var prod in ans) {
    Console.WriteLine($"Key: {prod.ProductId}");
    Console.WriteLine($"\t{prod.ProductId}, {prod.PartNumber}, Total Orders({prod.TotalOrders}) - Total Quantity ({prod.TotalSold})");

    foreach (var m in prod.MarketPlaceList) {
        Console.WriteLine($"\t\t{m.SiteName}, Total Orders({m.SiteTotalOrders}) - Total Quantity ({m.SiteTotalQuantity})");
    }
}