Cyril Gandon
Cyril Gandon

Reputation: 17068

Simpliest way to transform a Dictionary<int, Dictionary<int, Foo> > to IEnumerable<IEnumerable<Foo> > in Linq?

I have an object Dictionary<int, Dictionary<int, Foo> > on one side, and a function taking a IEnumerable<IEnumerable<Foo> > on the other side.

What is the simpliest way to transform from one to the other in Linq, I can't think of something nice, all my tries ended with foreach loop...

Upvotes: 1

Views: 190

Answers (4)

Henk Holterman
Henk Holterman

Reputation: 273844

The result from

   var list = dict.Values.Select(kv => kv.Values);

is assignable to a parameter of type IEnumerable<IEnumerable<Foo> >.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1503799

This should do it:

return dictionary.Values.Select(nested => nested.Values);

Upvotes: 4

Arion
Arion

Reputation: 31249

Something like this:

var result= (
        from d in dic
        select
            (
                from values in d.Value
                select values.Value
            )
    );

Upvotes: 1

Kirill Polishchuk
Kirill Polishchuk

Reputation: 56222

Use:

var result = dict.Select(d => d.Value.Select(i => i.Value));

Upvotes: 1

Related Questions