List Distinct entries based on property and sum of duplicates

前端 未结 3 1175
感动是毒
感动是毒 2020-12-21 17:49

Im wondering I may be asking a simple question, but trying to get a elegant solution. I have a situation where below are the rows

State Label <

相关标签:
3条回答
  • 2020-12-21 18:30

    Not tested, but this should work.

    var newList = list.GroupBy(x=> new {x.State , x.Label })
                      .Select(x=> new YourClass(x.Key.State, x.Key.Label, x.Sum(x=>x.Value) ))
                      .ToList();
    
    0 讨论(0)
  • 2020-12-21 18:35

    On solution is using grouping:

    var result = from o in listObj
                 group o by o.State into g
                 select new ValueLabels() 
                 { 
                     State = g.Key, 
                     Label = g.First().Label,
                     Value = g.Sum(i => i.Value)
                 };
    
    0 讨论(0)
  • 2020-12-21 18:46

    try this :

    MyListOfComplextData.GroupBy(r => r.state) .Select(a => new { sum= a.Sum(b => b.code), Name = a.Key}) .ToList();

    0 讨论(0)
提交回复
热议问题