Compare the two lists with the Except method

I am trying to compare two lists with the Except method, but it does not work correctly:

List<Customer> PotentialSharedCustomer = new List<Customer>(); PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "01234", Name = "Hans Jürgen" }); PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "05465", Name = "Beate Müller" }); PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "15645", Name = "Sabine Meyer" }); PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "54654", Name = "Moritz Kummerfeld" }); PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "15647", Name = "Hanna Testname" }); List<Customer> ActualSharedCustomer = new List<Customer>(); ActualSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "01234", Name = "Hans Jürgen" }); ActualSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "05465", Name = "Beate Müller" }); ActualSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "15645", Name = "Sabine Meyer" }); PrepareCreateSharedCustomer(PotentialSharedCustomer, ActualSharedCustomer); public void PrepareCreateSharedCustomer(List<Customer> potentialSharedCustomer, List<Customer> actualSharedCustomer) { List<Customer> result = potentialSharedCustomer.Except(actualSharedCustomer).ToList<Customer>(); } 

The result variable should result in all PotentialSharedCustomers entries, they are not in the ActialSharedCustomer list:

 PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "54654", Name = "Moritz Kummerfeld" }); PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "15647", Name = "Hanna Testname" }); 

I think that “except” is the right way to solve this problem, but I get a return of all “PotentialSharedCustomer” items

thanks for reference

+6
source share
3 answers

One way without overriding Equals or writing a custom IEqualityComparer is to get a list of identifiers, and then filter the list:

 List<string> accountNumbers = potentialSharedCustomer.Select(c => c.AccountNumber) .Except(actualSharedCustomer.Select(c => c.AccountNumber)); List<Customer> result = potentialSharedCustomer.Where(c => accountNumbers.Contains(c.AccountNumber)); 

You can look at other data structures, such as HashSet , to improve search performance, but if the size is small, this may be enough.

+7
source

Customer must implement IEquatable<Customer> , which means you must implement public bool Equals(Customer other) (which return true if other is this ) and public int GetHashCode() .

Check out the documentation for examples: http://msdn.microsoft.com/en-us/library/bb300779.aspx

There's also this other StackOverflow answer that illustrates how to make GetHashCode() implementation: fooobar.com/questions/1250 / ....

+2
source
 var distinctCustomer = PotentialSharedCustomer .Select(p => p.AccountNumber) .Except(actualSharedCustomer.Select(q => q.AccountNumber)) .ToList(); 
-1
source

Source: https://habr.com/ru/post/955988/


All Articles