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
source share