C# Compare Two Lists of Different Objects -
this question has answer here:
i saw quickest way compare 2 list<> i'm having trouble adapting situation. issue lists of different types.
my lists this:
list<type1> firstlist; list<type2> secondlist;
here have now:
foreach (type1 item in firstlist) { if (!secondlist.any(x => x.id == item.id)) { // code executed on each item in firstlist not in secondlist } } foreach (type2 item in secondlist) { if (!firstlist.any(x => x.id == item.id)) { // code executed on each item in secondlist not in firstlist } }
this works , all, o(n^2)
. there way make more efficient? solution in questions linked above says use .except
doesn't take lambda.
edit: mentioned above, still being flagged duplicate. not have 2 lists of same object. have 2 lists of different objects. type1 , type2 different types. both have id need match on.
i recommend converting ids of 2 types 2 hashsets. can
hashset<int> = new hashset<int>(firstlist.select(o => o.id)); hashset<int> b = new hashset<int>(secondlist.select(o => o.id)); if (a.issubsetof(b) && b.issubsetof(a)) { //do thing }