我想兩個列表與除方法比較,但它不工作正確的:比較兩個清單除方法
List<Customer> PotentialSharedCustomer = new List<Customer>();
PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "", 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 = "", 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>();
}
變量「結果」的結果應該是所有記錄「 PotentialSharedCustomers」,也有不在列表中‘ActialSharedCustomer’:
PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "54654", Name = "Moritz Kummerfeld" });
PotentialSharedCustomer.Add(new Customer { AccountId = Guid.Empty, AccountNumber = "15647", Name = "Hanna Testname" });
我thougt‘除’就是要解決這個問題的正確方法,但我得到的所有項目的回報‘PotentialSharedCustomer’
THX求助
您是否在'Customer'中覆蓋'Equals'和'GetHashCode'? – 2kay
[LINQ Except運算符和對象相等]的可能重複(http://stackoverflow.com/questions/693324/linq-except-operator-and-object-equality) –
如何比較客戶的兩個實例之間的相等性?如果你沒有做任何特別的事情,那麼你會得到參考平等比較的結果。我假設您需要提供一個IEqualityComparer,它根據帳號確定相等性。 –
Matt