7

我有一个包含以下属性的类:

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

我也有 2 个列表:

List<SomeClass> A
List<SomeClass> B

列表 A 有数据:

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

清单 B 有数据:

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

我想将 A 和 B(如果可能,使用 LINQ)合并到 SomeCalss 的列表 C 中,其中包含如下数据:

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

我怎样才能做到这一点?

4

2 回答 2

12

用于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)
                                        });
于 2012-12-16T12:34:46.270 回答
1

请参阅LINQ - 完全外部联接(SO)。

通过进行左外连接和右外连接,然后将这两者结合起来,您应该会得到您想要的东西。

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);
于 2012-12-16T12:40:46.580 回答