Linq

Projection Operators

Query

public void Linq14()

{

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 };

Console.WriteLine("Pairs where a < b:");

foreach (var pair in pairs)

{

Console.WriteLine("{0} is less than {1}", pair.a, pair.b);

}

}

Lambda Expression

public void DataSetLinq14()

{

var numbersA = testDS.Tables["NumbersA"].AsEnumerable();

var numbersB = testDS.Tables["NumbersB"].AsEnumerable();

var pairs = numbersA.SelectMany(a => numbersB, (a, b) => new {a, b})

.Where(@t => @t.a.Field("number") < @t.b.Field ("number"))

.Select(@t => new {a = @t.a.Field("number"), b = @t.b.Field ("number")});

Console.WriteLine("Pairs where a < b:");

foreach (var pair in pairs)

{

Console.WriteLine("{0} is less than {1}", pair.a, pair.b);

}

}

Output

Pairs where a < b:

0 is less than 1

0 is less than 3

0 is less than 5

0 is less than 7

0 is less than 8

2 is less than 3

2 is less than 5

2 is less than 7

2 is less than 8

4 is less than 5

4 is less than 7

4 is less than 8

5 is less than 7

5 is less than 8

6 is less than 7

6 is less than 8

0 is less than 1

0 is less than 3

0 is less than 5

0 is less than 7

0 is less than 8

2 is less than 3

2 is less than 5

2 is less than 7

2 is less than 8

4 is less than 5

4 is less than 7

4 is less than 8

5 is less than 7

5 is less than 8

6 is less than 7

6 is less than 8

Related Tutorial