A simple LinQ question: convert [] [] to []

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

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

Can someone choose me a solution to do this?

PS is just a LinQ solution :)

+3
source share
4 answers

You need to use SelectManyto combine result sets.

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

For what it's worth, it can be written a little 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);
+5
source

Enumerables.SelectMany(), . .

+3

Source: https://habr.com/ru/post/1741262/


All Articles