c# - IEnumerable.Except() not excluding -


i have 2 arrays. first holds members, second holds claimed members. i'm trying unclaimed members using except, resulting array includes members (an exact copy of first array).

var junior_handler_all = db2.junior_handler.include(j => j.person).include(j => j.status).toarray();  var junior_handler_claimed = db.junior_handler.include(j => j.person).include(j => j.status).toarray();  var junior_handler_unclaimed = junior_handler_all.except(junior_handler_claimed).toarray(); 

i have tried getting unclaimed members using query same results.

var junior_handler_unclaimed = junior_handler_all.where(i => !junior_handler_claimed.contains(i.junior_handler_id.tostring())); 

any ideas why either of these aren't working?

the way except , contains compare elements calling gethashcode() , - if hash equal - calling equals check if 2 elements equal.

so if apply reference types not have own implementation of gethashcode() , equals(), comparison results in simple reference equality check.

you don't want compare references of objects, kind of id. have 2 options:

you can either override gethashcode() , equals() in class:

public class person {     public int id { get; set; }      public override bool equals(object o)     {         return id == (o person)?.id;     }     public override int gethashcode()     {         return id;     } } 

or implement iequalitycomparer<person> this:

public class comparer : iequalitycomparer<person> {     public bool equals(person x, person y)     {         return x.id == y.id; // or whatever defines equality     }     public int gethashcode(person obj)     {         return obj.id; // or whatever makes hash     } } 

and provide instance of comparer except:

var junior_handler_unclaimed =      junior_handler_all.except(junior_handler_claimed, new comparer()).toarray(); 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -