c# - How to use the IEqualityComparer -



c# - How to use the IEqualityComparer -

i have bells in database same number. want of them without duplication. create compare class work, execution of function makes big delay function without distinct, 0.6 sec 3.2 sec!

am doing right or have utilize method?

reg.addrange((from in this.datacontext.reglements bring together b in this.datacontext.clients on a.id_client equals b.id a.date_v <= datefin && a.date_v >= datedeb 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) { homecoming true; } else { homecoming false; } } public int gethashcode(class_reglement codeh) { homecoming 0; } }

no wonder, considering gethashcode implementation returns same value. distinct relies on hash function work efficiently.

in code, solution forwards gethashcode class_reglement.numf.gethashcode , implement appropriately there.

furthermore, equals code abomination should compressed 1 expression:

class compare : iequalitycomparer<class_reglement> { public bool equals(class_reglement x, class_reglement y) { homecoming x.numf == y.numf; } public int gethashcode(class_reglement codeh) { homecoming codeh.numf.gethashcode(); } }

furthermore, tolist phone call unnecessary , time-consuming. addrange accepts ienumerable conversion list isn’t required. asenumerable also redundant here since processing result in addrange cause anyway.

all in all, seems lot of code written without understanding (this called cargo cult programming). may seem work temporarily doesn’t work long-term: take time-out , seek understand writing, preferably while reading book on c#.

c# linq iequalitycomparer

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -