O'Reilly logo

Programming Reactive Extensions and LINQ by Paul Betts, Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required