2

Can't really understand how the select extension method works with a list inside another list, like this:

var queries = (from item in list
               from item2 in list.anotherlist
               select item2).ToList<MyType>();

This will not work:

// Gives me a list of List<QueryResult>
var queries = list.Select(item => item.anotherlist).ToList(); 

2 Answers 2

5

The SelectMany operator ought to do the trick - in this case, it takes a list of lists and flattens it:

var queries = list.SelectMany(sublist => sublist).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

+1. Just for completeness' sake, I'll add that from … from … select … in a LINQ expression is actually equivalent to SelectMany; it won't translate to Select. See e.g. this query expression translation cheat sheet by Bart de Smet or the C# 3.0 specification, section 7.15.2.4.
0

use selectmany

var queries = list.SelectMany(l => l.anotherList).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.