LINQ复合选择问题

Chr*_*ris 5 c# linq

我无法获得LINQ复合选择编译.这是代码:

int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };

var pairs =
    from a in numbersA,
            b in numbersB
    where a < b
    select new {a, b};
Run Code Online (Sandbox Code Playgroud)

代码来自此处的教程,标题为"SelectMany - Compound from 1":

http://msdn.microsoft.com/en-us/vcsharp/aa336758.aspx#SelectSimple1

我得到的编译时错误如下:

查询主体必须以select子句或group子句结束

紧跟在'numbersA'之后的逗号是发生错误的地方.现在我无法弄清楚我做错了什么,因为这只是MS网站上的代码.任何帮助都会非常感谢.

Meh*_*ari 12

您的代码不是有效的LINQ表达式.from子句仅支持单个集合.你应该重复整个from条款.你可能想说:

var pairs = from a in numbersA
            from b in numbersB
            where a < b
            select new {a, b};
Run Code Online (Sandbox Code Playgroud)


CMS*_*CMS 10

使用SelectMany的等效流畅语法,仅用于记录:

var pair = numbersA.SelectMany(a => numbersB, (a, b) => new {a, b})
                   .Where(n => n.a < n.b);
Run Code Online (Sandbox Code Playgroud)