Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ compound select problem

Tags:

c#

linq

I'm having trouble getting a LINQ compound select to compile. Here is the code:

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};

The code is from a tutorial from here, under the heading 'SelectMany - Compound from 1':

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

And the compile-time error i get is as follows:

A query body must end with a select clause or a group clause

The comma just after 'numbersA' is where the error occurs. Now i can't figure out what i've done wrong, since this is just code as per the MS website. Any help would be great thanks.

like image 256
Chris Avatar asked Mar 25 '09 23:03

Chris


2 Answers

Your code is not a valid LINQ expression. from clause supports a single collection only. You should repeat the whole from clause. You probably meant to say:

var pairs = from a in numbersA
            from b in numbersB
            where a < b
            select new {a, b};
like image 149
mmx Avatar answered Oct 05 '22 15:10

mmx


The equivalent fluent syntax using SelectMany, just for the record:

var pair = numbersA.SelectMany(a => numbersB, (a, b) => new {a, b})
                   .Where(n => n.a < n.b);
like image 25
Christian C. Salvadó Avatar answered Oct 05 '22 16:10

Christian C. Salvadó