13.3. Introducing Query Syntax

The query you saw in the previous example is quite simple; it requests all the authorized reviews from the system and returns them in a sorted order. However, the querying capabilities of LINQ are much more powerful than this. In this section you learn more about the LINQ query syntax that you use to query your object model.

13.3.1. Standard Query Operators

LINQ supports a large number of query operators — keywords that allow you to select, order, or filter data that is to be returned from the query. Although all of the examples in this chapter are discussed in the context of LINQ to SQL, you can easily apply them to the other LINQ implementations as well. In the following section you get an overview of the most important standard query operators followed by an example. Each of the examples uses the object model and the DataContext object called myDataContext you created earlier as the data source to query against.

13.3.1.1. Select

The Select keyword (select in C#) is used to retrieve objects from the source you are querying. In this example you see how to select an object of an existing type. Later in this chapter you see how to define new object shapes on the fly.

VB.NET

Dim allReviews = From r In myDataContext.Reviews _
                 Select r

C#

var allReviews = from r in myDataContext.Reviews
                 select r;

The r variable in this example is referred to as a range variable that is only available within the current query. You typically introduce the range variable ...

Get Beginning ASP.NET 3.5: In C# and VB 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.