Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use LINQ to combine a property in a list of lists?

Tags:

c#

linq

I have a Dictionary that looks like such: Dictionary<Search_Requests, List<Tuple<Search_Subjects, SearchData>>>

In the SearchData class, there's a property called SearchCode. What I want to do is get an array of every search code that appears in this dictionary. I could do this with a few loops, but I'd really prefer to use LINQ. Unfortunately, I can't wrap my mind around how to do this. I tried

RequestDictionary.Select(s => s.Value.Select(z => s.Value.Select(x => x.Item2.SearchCode).ToArray()).ToArray()).ToArray();

But that just got me a string[][][], which isn't close to what I wanted. Can I get a push in the right direction?

like image 435
cost Avatar asked Feb 18 '14 22:02

cost


1 Answers

You can use .SelectMany() to flatten the results:

RequestDictionary
    .SelectMany(s 
        => s.Value.SelectMany(z => s.Value.Select(x => x.Item2.SearchCode))
    .ToArray();
like image 50
BartoszKP Avatar answered Nov 09 '22 05:11

BartoszKP