使用LinQ汇总属性值

我有一个名为Product的自定义对象列表:List


class Product

{

    string Key1 {get; set;}

    string Key2 {get; set;}

    int Count1 {get; set;}

    int Count2 {get; set;}

}

我要合并多个产品列表,并且需要创建一个新列表,该列表将具有每个Count属性的总和值。例如


List 1:

"Key1", "Key2", 1, 2

"Key2", "Key3", 3, 4


List 2:

"Key1", "Key2", 5, 6

"Key2", "Key3", 7, 8

所以我的新清单应该是:


New List:

"Key1", "Key2", 6, 8

"Key2", "Key3", 10, 12

有人可以帮我吗?


呼啦一阵风
浏览 177回答 2
2回答

慕标5832272

你可以这样做var list1 = new List<Product>()&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; new Product(){Key1 = "Key1", Key2 ="Key2", Count1 = 1, Count2 = 2},&nbsp; &nbsp; &nbsp; new Product(){Key1 = "Key2", Key2 ="Key3", Count1 = 1, Count2 = 2}&nbsp; &nbsp;};var list2 = new List<Product>()&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; new Product(){Key1 = "Key1", Key2 ="Key2", Count1 = 6, Count2 = 8},&nbsp; &nbsp; &nbsp; new Product(){Key1 = "Key2", Key2 ="Key3", Count1 = 10, Count2 = 12}&nbsp; &nbsp;};var result = list1.Concat(list2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(x => new {x.Key1,x.Key2})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(x => new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.Key.Key1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x.Key.Key2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SumCount1 = x.Sum(y => y.Count1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SumCount2 = x.Sum(y => y.Count2)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}).ToList();输出在这里演示Key1, Key2, 7, 10Key2, Key3, 11, 14其他资源List.AddRange将指定集合的元素添加到列表的末尾。Enumerable.GroupBy方法(IEnumerable,Func,Func)根据指定的键选择器功能对序列的元素进行分组,并使用指定的功能为每个组投影元素。Enumerable.Concat方法(IEnumerable,IEnumerable)连接两个序列。

哈士奇WWW

List<Product> lst1 = new List<Product>();&nbsp;List<Product> lst2 = new List<Product>();&nbsp;lst1.Add(new Product() {Key1 = "K1",Key2 ="K2", Count1 =1, Count2=2 });&nbsp;lst1.Add(new Product() { Key1 = "K2", Key2 = "K3", Count1 = 3, Count2 = 4 });&nbsp;lst2.Add(new Product() { Key1 = "K1", Key2 = "K2", Count1 = 5, Count2 = 6});&nbsp;lst2.Add(new Product() { Key1 = "K2", Key2 = "K3", Count1 = 7, Count2 = 8 });// Way 1&nbsp;var l = lst1.Join(lst2, l1 => l1.Key1, l2 => l2.Key1,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (lt1, lt2) => new Product { Key1 = lt1.Key1, Key2 = lt1.Key2, Count1 = lt1.Count1 + lt2.Count1, Count2 = lt1.Count2 + lt2.Count2 } ).ToList() ;// Way 2var result = lst1.Join(lst2, x => new { x.Key1, x.Key2 },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;y => new { y.Key1, y.Key2 }, (x, y) =>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new Product { Key1 = x.Key1, Key2 = x.Key2, Count1 = x.Count1 + y.Count1, Count2 = x.Count2 + y.Count2 }).ToList();
打开App,查看更多内容
随时随地看视频慕课网APP