Hello LINQ
Let's take a look at another very simple LINQ program in which we extract the even numbers from a list of numbers (which, admittedly, can be done even more easily without LINQ!), as shown in Listing 1-1.
Listing 1-1. Not Hello World
List<int> ints = new List<int>()
{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
var query = from i in ints
where i % 2 == 0
select i;
foreach ( var j in query )
Console.Write ("{0} ", j);
The output is as follows:
2 4 6 8 10 12 14
The structure of this program is basic to almost any LINQ program. We begin with an enumerable collection (our List
of integers). The heart of the program is the LINQ query, which begins (always) with a from
statement and ends (always) with a ...
Get Programming Reactive Extensions and LINQ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.