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 =
from a in numbersA
from b in numbersB
where a < b
select new { a, b };
C# language specification. See also. You use a lambda expression to create an anonymous function. Use the lambda declaration operator => to separate the lambda's parameter list from its body. A lambda expression can be of any of the following two forms: Expression lambda that has an expression as its body: C#.
Expression lambda that has an expression as its body: Statement lambda that has a statement block as its body: To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator and an expression or a statement block on the other side.
Use the lambda declaration operator => to separate the lambda's parameter list from its body. A lambda expression can be of any of the following two forms: Expression lambda that has an expression as its body: Statement lambda that has a statement block as its body:
Statement lambda that has a statement block as its body: To create a lambda expression, you specify input parameters (if any) on the left side of the lambda operator and an expression or a statement block on the other side. Any lambda expression can be converted to a delegate type.
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.
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