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

Get Programming Reactive Extensions and LINQ now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.