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