What is equivalent of following code snippet in lambda expression?
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
var pairs =
开发者_如何学Pythonfrom a in numbersA
from b in numbersB
where a < b
select new { a, b };
Here is a LINQ expression using method syntax (as opposed to query syntax):
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
pairs = numbersA
.SelectMany(_ => numbersB, (a, b) => new { a, b })
.Where(x => x.a < x.b);
The original query is translated into this:
int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };
int[] numbersB = { 1, 3, 5, 7, 8 };
pairs = numbersA
.SelectMany(_ => numbersB, (a, b) => new { a, b })
.Where(x => x.a < x.b)
.Select(x => new { x.a, x.b });
However the last Select
isn't required and can be removed.
精彩评论