I have some bells in my database with the same number. I want to get all of them without duplication. I created a compare class to do this work, but the execution of the function causes a big delay from the function without distinct, from 0.6 sec to 3.2 sec!
Am I doing it right or do I have to use another method?
reg.AddRange(
(from a in this.dataContext.reglements
join b in this.dataContext.Clients on a.Id_client equals b.Id
where a.date_v <= datefin && a.date_v >= datedeb
where a.Id_client == b.Id
orderby a.date_v descending
select new Class_reglement
{
nom = b.Nom,
code = b.code,
Numf = a.Numf,
})
.AsEnumerable()
.Distinct(new Compare())
.ToList());
class Compare : IEqualityComparer<Class_reglement>
{
public bool Equals(Class_reglement x, Class_reglement y)
{
if (x.Numf == y.Numf)
{
return true;
}
else { return false; }
}
public int GetHashCode(Class_reglement codeh)
{
return 0;
}
}
Your
GetHashCodeimplementation always returns the same value.Distinctrelies on a good hash function to work efficiently because it internally builds a hash table.When implementing interfaces of classes it is important to read the documentation, to know which contract you’re supposed to implement.1
In your code, the solution is to forward
GetHashCodetoClass_reglement.Numf.GetHashCodeand implement it appropriately there.Apart from that, your
Equalsmethod is full of unnecessary code. It could be rewritten as follows (same semantics, ¼ of the code, more readable):Lastly, the
ToListcall is unnecessary and time-consuming:AddRangeaccepts anyIEnumerableso conversion to aListisn’t required.AsEnumerableis also redundant here since processing the result inAddRangewill cause this anyway.1 Writing code without knowing what it actually does is called cargo cult programming. It’s a surprisingly widespread practice. It fundamentally doesn’t work.