Yes, that is SelectMany
. Let's look at a simple example:
class C
{
public string Name { get; set; }
public IEnumerable<string> Subs { get; set; }
}
Now let's make a bunch of them.
var cs = new List<C> {
new C { Name = "Alice", Subs = new List<string> { "Orange", "Green" }},
new C { Name = "Bobby", Subs = new List<string> { "Red", "Blue" }}
}
That is, we have
Alice
Orange
Green
Bobby
Red
Blue
You want a new list that is
Alice
Orange
Alice
Green
Bobby
Red
Bobby
Blue
Right?
Any time you want to "flatten" a list-of-lists you use SelectMany. Either in comprehension form:
var query =
from c in cs
from s in c.Subs
select new C { Name = c.Name, Subs = new List<string> { s } };
Or in fluent form
var query =
cs.SelectMany(
c => c.Subs,
(c, s) => new C { Name = c.Name, Subs = new List<string> { s } });
which as you can see has the same structure, just a little harder to read.
If you then need a list out the other end:
var newList = query.ToList();
And you're done.