Home >Backend Development >C++ >How Can I Flatten a Nested List in LINQ Using SelectMany()?
When processing the nested list in Linq, sometimes multiple lists need to be converted into a list of a single display. This can be implemented using the
method.
SelectMany()
Scene:
Considering the following linq query returning the list of nested integer:
If you need to output a single list containing all elements in the nested list, you can modify the inquiry as follows:
<code class="language-csharp">IEnumerable<List<int>> iList = (from number in (from no in Method() select no) select number).ToList();</code>
Each element of the input sequence (in this example) is projected into a new sequence. By specifying the parameter , each element of the nested list becomes a new element in the output sequence.
<code class="language-csharp">var result = iList.SelectMany(i => i);</code>Example:
SelectMany()
i
For the source list [1, 2, 3, 4] and [5, 6, 7], the modified query will generate the following display list:
Therefore, using the method can be converted into a single display list in LINQ.
The above is the detailed content of How Can I Flatten a Nested List in LINQ Using SelectMany()?. For more information, please follow other related articles on the PHP Chinese website!