sobota, 12 listopada 2016

LINQ - Find common items across multiple lists

Given the following: List<List<Option>> optionLists

What would be a quick way to find the subset of Option objects that appear in all N lists?
Equality is determined through some string property such as option1.Value == option2.Value.
We should end up with List<Option> where each item appears only once.

Answear:

var x = from list in optionLists
            from option in list
            where optionLists.All(l => l.Any(o => o.Value == option.Value))
            orderby option.Value
            select option;

Brak komentarzy:

Prześlij komentarz