Search code examples
c#linqlinq-method-syntax

Compare 2 Lists of numbers in LINQ Method Syntax


I have 2 lists of numbers.

public int[] numbersA = { 0, 2, 4 };
public int[] numbersB = { 1, 3, 5 }; 

I need output like below

Expected Result

0 is less than 1
0 is less than 3
0 is less than 5

2 is less than 3
2 is less than 5

4 is less than 5

How is it possible through LINQ method syntax?


Solution

  • With Method syntax:

    var result = numbersA.SelectMany(c => numbersB, (c, o) => new { c, o })
                         .Where(d => d.c < d.o)
                         .Select(v=>  v.c + "is less than"+ v.o);