I’m using Linq-to-Sql to query a SQL Server database. This query returns a List of an entity in my database. My underlying data is NOT changing.
Once I have received the List, I call GetHashCode on it in order to test for equality. Oddly, the hash value is always different. Why would it always be different?
Thank you,
Are different beacuse they are different object references.
You need to override Equals() and GetHashCode() for your object, based on the object data, if you want to behave in that way.
Here you have an example about how to do it, and here a blog post about the guidelines overriding the
GetHashCode()method. Hope it helps.As Servy said in his comment, keep in mind that even overriding
GetHashCode()method, you won’t be able to have a collision-free hash with that type of data (ever), you can only reduce the collision rate. You need to useEquals()to ensure objects with the same hash are really the same