Maxim Polishchuk
Maxim Polishchuk

Reputation: 334

Simple LinQ question: convert [][] to []

I would like to get collection or list of Item objects, but I get array of arrays now.

Item[][] s = employees.Select(e => e.Orders.Select(o => new Item(e.ID, o.ID)).ToArray()).ToArray();

Can anyone select me solution to do it?

P.S. just LinQ solution :)

Upvotes: 3

Views: 373

Answers (4)

Will Vousden
Will Vousden

Reputation: 33388

For what it's worth, this can be written somewhat more succinctly and clearly in LINQ syntax:

var s = from e in employees
        from o in e.Orders
        select new Item(e.ID, o.ID);

Upvotes: 5

Paul Ruane
Paul Ruane

Reputation: 38610

Have a look at Enumerables.SelectMany(), see the example in this Stack Overflow question.

Upvotes: 3

Nick Larsen
Nick Larsen

Reputation: 18877

You need to use SelectMany to concatenate result sets.

var items = employees.SelectMany(e => e.Orders.Select(o => new Item(e.ID, o.ID)));

Upvotes: 5

Niki
Niki

Reputation: 15867

The extension method you're looking for is called "SelectMany": http://msdn.microsoft.com/en-en/library/system.linq.enumerable.selectmany.aspx

Upvotes: 3

Related Questions