Sto cercando di confrontare due liste con l'eccezione di metodo, ma non funziona correttamente:Confronta due liste con eccezione metodo
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>();
}
Il risultato del "risultato" variabile dovrebbe essere tutti i record di " PotentialSharedCustomers", non ci sono nella lista '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" });
I thougt 'Salvo' è il modo corretto per risolvere questo, ma ottengo il ritorno di tutte le voci di 'PotentialSharedCustomer'
Thx per aiuto
Hai sovrascritto 'Equals' e' GetHashCode' in 'Cliente'? – 2kay
possibile duplicato di [LINQ Tranne operatore e uguaglianza oggetto] (http://stackoverflow.com/questions/693324/linq-except-operator-and-object-equality) –
Come si confronta l'uguaglianza tra due istanze del Cliente? Se non stai facendo nulla di speciale, otterrai i risultati di un confronto di uguaglianza di riferimento. Presumo che sia necessario fornire a IEqualityComparer che determina l'uguaglianza in base al numero di conto. –
Matt