Combining 2 lists and combining multiple properties using LINQ

I have a class that 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:

 List<SomeClass> A List<SomeClass> B 

List A has data:

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

List B has data:

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

I want to combine A and B (using LINQ, if possible) in C List SomeCalss, which contains the following data:

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

How can I achieve this?

+7
source share
2 answers

Use GroupBy to group common objects and Sum to summarize required properties.

  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) }); 
+10
source

See LINQ - Complete External Connection (SO).

By doing the left outer join and the right outer join, and then taking the union of the two, you should get what you are 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); 
+1
source

All Articles