linq - How to find Difference in two Collections<T> in C# -
i have 2 collections of following class
public class abc { public int studentid {get;set;} public int schoolid {get;set;} // class has other properties both above 2 keys } now have 2 collections of abc
icollection<abc> c1 = {some data} icollection<abc> c2 = {some data} i want find objects of abc in c1 not in c2 based on keys i-e studentid , schoolid
use except
var diff = c1.except(c2) please note, in order track equality can override equals method, or implement , pass iequalitycomparer except method
class abcequalitycomparer : iequalitycomparer<abc> { public bool equals(abc b1, abc b2) { return (b1.studentid == b2.studentid) && (b1.schoolid == b2.schoolid) } public int gethashcode(abc b) { return 7*b.studentid.gethashcode() + b.schoolid.gethashcode(); } } than can use
var diff = c1.except(c2, new abcequalitycomparer())
Comments
Post a Comment