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.
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};
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With