I'm trying to import data from a CSV file, unfortunately there is no primary key that would allow me to uniquely identify a given row. So I created a dictionary in which the key is the value that GetHashCode returns to me. I use the dictionary because its search is much faster than searching with linq and where with conditions for several properties.
My GetHashCode override looks like this:
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = hash * 23 + this.Id.GetHashCode();
hash = hash * 23 + this.Author?.GetHashCode() ?? 0.GetHashCode();
hash = hash * 23 + this.Activity?.GetHashCode() ?? 0.GetHashCode();
hash = hash * 23 + this.DateTime?.GetHashCode() ?? 0.GetHashCode();
return hash;
}
}
After fetching data from DB I do:
.ToDictionary(d => d.GetHashCode());
And here comes the problem, I checked the database and I don't have any duplicates when it comes to these four parameters. But when running the import I often get an error that the given key already exists in the dictionary, but if I run the import again for the same data the next time everything runs fine.
How can I fix this error? The import application is written in .net 5
Id - long
Author, Activity - string
DateTime - DateTime?
Unfortunately, this ID is more like FK is not unique, there may be many rows with the same id, author, activity, but e.g. a different datetime
0.GetHashCode()is always just0..ToDictionary(d => d.GetHashCode())is guaranteed to result in duplicate errors. Why are you using a hash as a key at all?if I run the import again for the same data the next time everything runs fineThe implication here is that one of the types on which you are callingGetHashCode()does not have a proper implementation for it. What are the concrete types ofId,AuthorandActivity? (I'm assuming thatDateTimereally is aDateTime)GetHashCodedoesn't need to provide different results for objects that are not considered equal by the implementation ofEquals. It only should do so in order to provide good performance on sorting and dictionary access.