Starting with a basic class:
public class Car
{
public string Name {get;set;}
}
I can then create a list of these cars
List<Car> cars = new List<Car>();
The new step is to have a List of this List, like this:
List<List<Car>> allListsOfCars = new List<List<Car>>();
After populating allListsOfCars, I want to pass it to a function which will return me the cars which exist in every List list.
I know it sounds confusing, so I’ll try explain a bit more.
If I have ListA, ListB, ListC all of type List – and now combine these into 1 holding list(The list of a list), then how can I get back all the cars that exist in every list? For example if a car only exists in ListA, then I’m not interested, it needs to exist in ListA AND ListB AND ListC, then I want it added to the result set and returned.
Thanks in advance.
You need to find the composite intersection of all the sublists.
It might be possible to rewrite using the
Aggregateoperator to eliminate the loop, butAggregatenever reads very well IMO.If the lists are long, you’ll probably get a decent speed increase using a
HashSetMake sure that you
Carclass implements equality members andGetHashCodecorrectly, otherwise neither of these approaches will work as expected.