c# - Intersection of List of List -


i have list of lists looks following

public class filteredvm {     public int id { get; set; }     public string name { get; set; }     public string number { get; set; } }   list<list<filteredvm>> groupedexpressionresults = new list<list<filteredvm>>(); 

i intersect lists within list based upon id's, whats best way tackle this?

here's optimized extension method:

public static hashset<t> intersectall<t>(this ienumerable<ienumerable<t>> series, iequalitycomparer<t> equalitycomparer = null) {     if (series == null)         throw new argumentnullexception("series");      hashset<t> set = null;     foreach (var values in series)     {         if (set == null)             set = new hashset<t>(values, equalitycomparer ?? equalitycomparer<t>.default);         else             set.intersectwith(values);     }      return set ?? new hashset<t>(); } 

use following comparer:

public class filteredvmcomparer : iequalitycomparer<filteredvm> {     public static readonly filteredvmcomparer instance = new filteredvmcomparer();      private filteredvmcomparer()     {     }      public bool equals(filteredvm x, filteredvm y)     {         return x.id == y.id;     }      public int gethashcode(filteredvm obj)     {         return obj.id;     } } 

like that:

series.intersectall(filteredvmcomparer.instance) 

you write

series.aggregate((a, b) => a.intersect(b, filteredvmcomparer.instance)) 

but 'd wasteful because it'd have construct multiple sets.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -