
.NET 3.5+Simple LINQ Queries (2)
The second part of the LINQ to Objects tutorial describes basic querying using LINQ features. The article discusses the "Where" standard query operator, known as the restriction operator, and the equivalent query expression syntax.
Restrictions and Query Expression Syntax
In this section we will recreate the queries from above using the Query Expression syntax. This is the more natural syntax that some developers prefer to use to generate queries. The two syntaxes are equally valid and both should be considered for their suitability according to the task being undertaken. For example, you may decide to use the extension methods when working with other C# developers and to use the query syntax when working closely with SQL users.
The basic form of the query expression syntax is as follows:
var results =
from range-variable in data-source
where predicate
select projection;
In this syntax, the results element receives the results of the query. As with the standard query operators, the result is usually an IEnumerable<T> object that contains all of the information required to generate the query's results. The results are usually generated using deferred execution when they are needed. Here I have declared the type for the results implicitly using the var keyword.
The from clause includes two important elements. The data source specifies the data structure that contains the items to be queried. The range variable is a variable that becomes available to the rest of the query to represent each item. In this article we will see how it is used to specify the predicate that filters the results. In future articles we will see how it can be used to determine the structure of the returned data and to control other operations, such as sorting.
The where clause provides the restrictions to apply when filtering the data source. The predicate is a Boolean expression that is very similar to the lambda expression used in earlier examples. The syntax differs slightly as no lambda operator or parameters are supplied.
The final section in this simple syntax is the select clause. The projection element determines which elements of the data source are returned. In this article we will return the entire object for each valid result. In the next article we will see how other projections are possible.
To demonstrate the syntax, we can recreate the earlier examples. The first example queried an array of strings containing the names of colours. Only colours with a string length of five or greater were included in the results. This can be expressed as a query as follows:
var longNames =
from c in colours
where c.Length >= 5
select c;
In the above code, the results are returned in the longNames variable. The range variable is named "c". It is common to see short or single-character range variable names, though longer names are acceptable and can improve code clarity. The where clause specifies that the length of c must be greater than or equal to five for an item from the data source, colours, to be included in the results. The select clause specifies that for each result, the "c" object will be returned.
Let's recreate the second sample, which retrieved all employees from a list if their job title included the word, "Developer":
var developers =
from e in employees
where e.Title.Contains("Developer")
select e;
Again, the syntax is reasonably simple. You can almost read the query as plain English: For each employee, if the Title property contains the string, "Developer", add it to the results.
To complete the conversions, we can express the more complex employee query using the query syntax. In this case Boolean operators were used to combine the predicate's conditions:
var developers =
from e in employees
where (e.Title.Contains("Developer") && e.Salary > 35000)
|| (!e.Title.Contains("Developer") && e.Salary < 25000)
select e;
21 June 2010