简体   繁体   English

使用LINQ合并2个列表并汇总多个属性

[英]Merging 2 lists and sum several properties using LINQ

I have an class which contains the following properties: 我有一个包含以下属性的类:

public class SomeClass()
{
public Int32 ObjectId1 {get;set;}
public Int32 ObjectId2 {get;set;}
public Int32 ActiveThickeness {get;set;}
public Int32 ActiveFilterThickness {get;set;}
}

I also have 2 lists: 我还有2个列表:

List<SomeClass> A
List<SomeClass> B

List A has data: 列表A有数据:

| ObjectId1 | ObjectId2 | ActiveThickness | ActiveFilterThickness |
-------------------------------------------------------------------
|     1     |     3     |       50        |           0           |
 ------------------------------------------------------------------
|     1     |     2     |       400       |           0           |
-------------------------------------------------------------------
|     4     |    603    |       27        |           0           |
-------------------------------------------------------------------

List B has data: 清单B有数据:

| ObjectId1 | ObjectId2 | ActiveThickness | ActiveFilterThickness |
-------------------------------------------------------------------
|     1     |     3     |       0         |         13671         |
 ------------------------------------------------------------------
|     1     |     2     |       0         |          572          |
-------------------------------------------------------------------
|    29     |    11     |       0         |         4283          |
-------------------------------------------------------------------

I want to merge A and B (using LINQ if possible) into List C of SomeCalss which contains data as followed: 我想将A和B(如果可能的话使用LINQ)合并到SomeCalss的List C中,其中包含如下数据:

| ObjectId1 | ObjectId2 | ActiveThickness | ActiveFilterThickness |
-------------------------------------------------------------------
|     1     |     3     |       50        |         13671         |
 ------------------------------------------------------------------
|     1     |     2     |       400       |          572          |
-------------------------------------------------------------------
|    29     |    11     |       0         |         4283          |
-------------------------------------------------------------------
|     4     |    603    |       27        |           0           |
-------------------------------------------------------------------

How can I achieve that? 我怎样才能做到这一点?

Use GroupBy to group common objects and Sum to sum required properties 使用GroupBy对公共对象进行分组,使用Sum对所需属性求和

        var ab = A.Concat(B).GroupBy(x => new
                                              {
                                                      x.ObjectId1,
                                                      x.ObjectId2
                                              });


        var result = ab.Select(x => new SomeClass
                                        {
                                                ObjectId1 = x.Key.ObjectId1,
                                                ObjectId2 = x.Key.ObjectId2,
                                                ActiveFilterThickness = x.Sum(i => i.ActiveFilterThickness),
                                                ActiveThickeness = x.Sum(i => i.ActiveThickeness)
                                        });

See LINQ - Full Outer Join (SO). 请参阅LINQ - 完全外部加入 (SO)。

By doing a left outer join and a right outer join, and then taking the union of those two, you should get what you're looking for. 通过左外连接和右外连接,然后取两者的结合,你应该得到你正在寻找的东西。

var leftOuterJoin = from someclass1 in A
                    join someclass2 in B
                    on someclass1.ObjectID2 equals someclass2.ObjectID2
                    into temp
                    from item in temp.DefaultIfEmpty(new SomeClass(){ objectID1 = someclass1.objectID1, ... })
                    select new SomeClass()
                    {
                        ...
                    };
var rightOuterJoin = from someclass2 in B
                     join someclass1 in A
                     on someclass1.ObjectID2 equals someclass2.ObjectID2
                    into temp
                    from item in temp.DefaultIfEmpty(new SomeClass(){ objectID1 = someclass1.objectID1, ... })
                    select new SomeClass()
                    {
                        ...
                    };
var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM