score:7

Accepted answer
list.select (x => x.item2 as ienumerable<someobject>)
    .aggregate((x,y)=> x.intersect(y))
    .tolist();

or, as jeppe stig nielsen suggested (and i think it's much more elegant):

list.select(x => x.item2.asenumerable())
    .aggregate(enumerable.intersect)
    .tolist();

score:-1

presuming that the class overrides equals + gethashcode or you have a custom iequalitycomparer<someobject> you can use following query which uses enumerable.all:

var result = list
    .selectmany(t => t.item2)   // select all objects
    .distinct()                 // just an optimization since duplicates are guaranteed
    .where(obj => list.all(t => t.item2.contains(obj))); 

here's my sample data:

var list = new list<tuple<string, list<someobject>>>();
list.add(tuple.create("a", new list<someobject> { new someobject { id = 1 }, new someobject { id = 2 }, new someobject { id = 4 } }));
list.add(tuple.create("b", new list<someobject> { new someobject { id = 1 }, new someobject { id = 2 }, new someobject { id = 3 } }));
list.add(tuple.create("c", new list<someobject> { new someobject { id = 1 }, new someobject { id = 2 }, new someobject { id = 3 } }));
list.add(tuple.create("d", new list<someobject> { new someobject { id = 1 }, new someobject { id = 2 }, new someobject { id = 3 } }));

only the someobjects with id = 1 or id = 2 are in all lists and that's the result of the query.

score:1

as i understand correctly, you need intersection of many lists:

var results = source.first().item2
foreach (var element in source.skip(1) )
{
    results = results.intersect(element.item2)
}

score:1

strongly inspired by juan lopes's beautiful answer, you can define this extension:

static ienumerable<tsource> intersectmany<tsource>(this ienumerable<ienumerable<tsource>> sources)
{
  return sources.aggregate(enumerable.intersect);
}

then this works:

var result = list.select(x => x.item2).intersectmany();

it works because ienumerable<out t> is covariant in t (c# 4, .net 4.0).


Related Query

More Query from same tag