【问题标题】:C# Linq Consolidate by ID and Sum QuantitiesC# Linq 按 ID 和总和数量合并
【发布时间】:2019-06-12 21:32:20
【问题描述】:

我有一个简单的设置(如下)。每个“组件”都包含一个零件列表。程序集中可能有重复的“零件”对象。

我的目标是合并零件对象(按 id)并对数量求和。

最终结果应如下所示(每个部分的数量相加):

[Assembly 1]
  [3] Part 1  
  [8] Part 2
  [4] Part 3

[Assembly 2]
  [3] Part 1
  [15] Part 3

查看下面“控制台输出”下的代码,了解我目前得到的信息。

我尝试了以下 LINQ(失败):

List<Assembly> listAssy2 = listAssy.SelectMany(a => a.listParts.GroupBy(b => b.qty)).ToList();

代码:

public class Part {
    public int id { get; set;}
    public string title { get; set; }
    public int qty { get; set; }
}

public class Assembly {
    public int id { get; set; }
    public string title { get; set; }
    public List<Part> listParts { get; set; }
}


public static void Main()
{       
    List<Assembly> listAssy = new List<Assembly>();

    // ----------------- ASSEMBLY 1 -----------------
    //List of Parts
    List<Part> partList1 = new List<Part>();
    partList1.Add(new Part { id = 1, title = "Part 1", qty = 2 }); 
    partList1.Add(new Part { id = 1, title = "Part 1", qty = 1 }); 
    partList1.Add(new Part { id = 2, title = "Part 2", qty = 2 });
    partList1.Add(new Part { id = 3, title = "Part 3", qty = 4 });
    partList1.Add(new Part { id = 2, title = "Part 2", qty = 6 });

    Assembly assy1 = new Assembly {id = 1, title = "Assembly 1", listParts = partList1};
    listAssy.Add(assy1);

    // ----------------- ASSEMBLY 2 -----------------
    //List of Parts
    List<Part> partList2 = new List<Part>();
    partList2.Add(new Part { id = 1, title = "Part 1", qty = 2 }); 
    partList2.Add(new Part { id = 3, title = "Part 3", qty = 4 }); 
    partList2.Add(new Part { id = 3, title = "Part 3", qty = 11 });
    partList2.Add(new Part { id = 1, title = "Part 1", qty = 1 });

    Assembly assy2 = new Assembly {id = 2, title = "Assembly 2", listParts = partList2};
    listAssy.Add(assy2);



    foreach (var assy in listAssy) {
        Console.WriteLine("[" + assy.title + "]");
        foreach (var part in assy.listParts) {
            Console.WriteLine("  [" + part.qty + "] " + part.title);    
        }   
        Console.WriteLine("");
    }

    /* ***** CONSOLE OUTPUT ******
      [Assembly 1]
        [2] Part 1
        [1] Part 1
        [2] Part 2
        [4] Part 3
        [6] Part 2

      [Assembly 2]
        [2] Part 1
        [4] Part 3
        [11] Part 3
        [1] Part 1 

    */

} 

【问题讨论】:

    标签: c# linq group-by


    【解决方案1】:

    应该这样做:

    var summaries = listAssy
        .Select(a => new {
            a.id,
            a.title,
            partQuantities = a.listParts.GroupBy(p => new { p.id, p.title })
                .Select(g => new { g.Key.id, g.Key.title, qty = g.Sum(p => p.qty)})
                .ToList()
        });
    
    foreach (var assy in summaries)
    {
        Console.WriteLine("[" + assy.title + "]");
        foreach (var part in assy.partQuantities)
        {
            Console.WriteLine("  [" + part.qty + "] " + part.title);
        }
        Console.WriteLine("");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 2018-07-21
      • 1970-01-01
      • 2017-09-12
      • 2017-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多