Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List.Contains is not working as hoped

If I have an object of type MyBull and a List<MyBull> orig:

// Just an example
MyBull x = getMeTheObjectWithIdFromDB(9);

orig.add(x);

// Again same? data object
MyBull y = getMeTheObjectWithIdFromDB(9);

Why is this false then?

// This is false, even though all the properties
// of x and y are the same.
orig.Contains<MyBull>(y); 
like image 489
VoodooChild Avatar asked Nov 27 '22 00:11

VoodooChild


1 Answers

By default objects will expose reference based equality. If you want custom rules, such as equality based on id fields, you need to override the Equals and GetHashCode methods.

like image 93
Samuel Neff Avatar answered Dec 15 '22 02:12

Samuel Neff