Is there a C# LINQ syntax for the Queryable.Select

2019-01-22 00:42发布

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(' ')

3条回答
我命由我不由天
2楼-- · 2019-01-22 01:12

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

查看更多
何必那么认真
3楼-- · 2019-01-22 01:21

You can use a Compound from Clause:

var tokens = from s in text
             from x in s.Split(' ')
             select x;
查看更多
Root(大扎)
4楼-- · 2019-01-22 01:23

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

var words = from str in text
            from word in str.Split(' ')
            select word;
查看更多
登录 后发表回答