Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove duplicates while merging lists using Union in LINQ

Tags:

c#

linq

I am trying to merge two lists using list.Union in LinqPad but I can't get it to work and wanted to check my understanding is correct.

Given this simple class:

public class Test 
{
   public int Id { get; set;}
   public int field1 { get; set; }

   public bool Equals(Test other)
   {        
      return this.Id.Equals(other.Id);
   }
}

And two lists populated like this:

List<Test> list = new List<Test>();
list.Add( new Test { Id = 1, field1 = 1});
list.Add( new Test { Id = 1, field1 = 2});
list.Add( new Test { Id = 2, field1 = 3});
list.Add( new Test { Id = 2, field1 = 4});

List<Test> list2 = new List<Test>();
list2.Add( new Test { Id = 1, field1 = 1});
list2.Add( new Test { Id = 1, field1 = 2});
list2.Add( new Test { Id = 2, field1 = 3});
list2.Add( new Test { Id = 2, field1 = 4});

I then try: var mergedList = list.Union(list2).ToList(); and output the data using a simple foreach loop and get this output:

ID: 1 -------- 1
ID: 1 -------- 2
ID: 2 -------- 3
ID: 2 -------- 4
ID: 1 -------- 1
ID: 1 -------- 2
ID: 2 -------- 3
ID: 2 -------- 4

I was under the impression that Union should remove the duplicates to return:

ID: 1 -------- 1
ID: 1 -------- 2
ID: 2 -------- 3
ID: 2 -------- 4

Am I doing something wrong or have I misunderstood?

Also, should it work without explicitly overriding the Equals method in the Test class?

Thanks

like image 230
davy Avatar asked Jun 07 '13 09:06

davy


People also ask

Does Union remove duplicates Linq?

Linq, acts upon 2 collections. It returns a new collection that contains the elements that are found. Union removes duplicates.

Is the Union method gets the unique elements from both the lists?

Union is an extension method to merge two collections. It requires at least two collections to perform the merge operation, but that merged collection holds only the distinct elements from both the collections.

How do I remove duplicates from a list in C sharp?

Use the Distinct() method to remove duplicates from a list in C#.


1 Answers

You can try something like that if not happy with default comparer (which is, in turn, utilizes GetHashCode method as @IlyaIvanov had mentioned):

// get all items that "other than in first list", so Where() and Any() are our filtering expressions
var delta = list2.Where(x2 => !list.Any(x1 => (x1.Id == x2.Id) && (x1.field1 == x2.field1)));

// now let merge two enumerables that have nothing "equal" between them
var merged = list.Union(delta).ToList();
like image 176
Yury Schkatula Avatar answered Sep 22 '22 14:09

Yury Schkatula