Is there a C# LINQ syntax for the Queryable.SelectMany() method?

C#LinqKeywordIqueryable

C# Problem Overview


When writing a query using C# LINQ syntax, is there a way to use the Queryable.SelectMany method from the keyword syntax?

For

string[] text = { "Albert was here", 
                  "Burke slept late", 
                  "Connor is happy" };

Using fluent methods I could query

var tokens = text.SelectMany(s => s.Split(' '));

Is there a query syntax akin to

var tokens = from x in text selectmany s.Split(' ')

C# Solutions


Solution 1 - C#

Yes, you just repeat the from ... in clause:

var words = from str in text
            from word in str.Split(' ')
            select word;

Solution 2 - C#

You can use a Compound from Clause:

var tokens = from s in text
             from x in s.Split(' ')
             select x;

Solution 3 - C#

Your query would be re-written as:

var tokens = from x in text
             from z in x.Split(' ')
             select z;

Here's a good page that has a couple of side-by-side examples of Lambda and Query syntax:

Select Many Operator Part 1 - Zeeshan Hirani

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrianCookseyView Question on Stackoverflow
Solution 1 - C#driisView Answer on Stackoverflow
Solution 2 - C#dtbView Answer on Stackoverflow
Solution 3 - C#Justin NiessnerView Answer on Stackoverflow