开发者

Compound Select using lambda expression

开发者 https://www.devze.com 2023-03-30 21:14 出处:网络
What is equivalent of following code snippet in lambda expression? int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 };

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消