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

LINQ Syntax

There are a number of flavors of LINQ, each tailored to work with different types of data. LINQ to Objects is designed to work with objects in memory, while LINQ to SQL is designed to work with SQL server-based data. The essence is the same, however, and LINQ to Objects shows that essence particularly well.

Consider the following statements, which you can copy and paste directly into LINQPad:

var primes = new List<int>() { 1, 2, 3, 5, 7, 11, 13, 17, 19 }; var query = from num in primes                         where num < 7                         select num;                  foreach ( var  i in query ) {    Console.WriteLine(i); }

The central three lines of code compose a LINQ query expression. The body of the query expression ...

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