tags:

views:

708

answers:

3

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.

+8  A: 

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};
Mehrdad Afshari
You got here first :) I'm voting you up.
Charlie Flowers
Thanks, i tried that and it works great. Pity that the MS site has invalid code!
Chris
+2  A: 

If I understand your intent correctly, then you need another from.

Like this:

var pairs =
    from a in numbersA // Comma removed from end of line here
    from b in numbersB // additional "from" keyword at start of line
    where a < b
    select new {a, b};
Charlie Flowers
That works, thanks! Sorry though, mehrdad got in first with the answer
Chris
+2  A: 

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);
CMS