Standard Query Operators Overview (C#)
The standard query operators are the methods that form the LINQ pattern. Most of these methods operate on sequences, where a sequence is an object whose type implements the IEnumerable<T> interface or the IQueryable<T> interface. The standard query operators provide query capabilities including filtering, projection, aggregation, sorting and more.
There are two sets of LINQ standard query operators: one that operates on objects of type IEnumerable<T>, another that operates on objects of type IQueryable<T>. The methods that make up each set are static members of the Enumerable and Queryable classes, respectively. They are defined as extension methods of the type that they operate on. Extension methods can be called by using either static method syntax or instance method syntax.
In addition, several standard query operator methods operate on types other than those based on IEnumerable<T> or IQueryable<T>. The Enumerable type defines two such methods that both operate on objects of type IEnumerable. These methods, Cast<TResult>(IEnumerable) and OfType<TResult>(IEnumerable), let you enable a non-parameterized, or non-generic, collection to be queried in the LINQ pattern. They do this by creating a strongly typed collection of objects. The Queryable class defines two similar methods, Cast<TResult>(IQueryable) and OfType<TResult>(IQueryable), that operate on objects of type IQueryable.
The standard query operators differ in the timing of their execution, depending on whether they return a singleton value or a sequence of values. Those methods that return a singleton value (for example, Average and Sum) execute immediately. Methods that return a sequence defer the query execution and return an enumerable object.
For methods that operate on in-memory collections, that is, those methods that extend IEnumerable<T>, the returned enumerable object captures the arguments that were passed to the method. When that object is enumerated, the logic of the query operator is employed and the query results are returned.
In contrast, methods that extend IQueryable<T> don't implement any querying behavior. They build an expression tree that represents the query to be performed. The query processing is handled by the source IQueryable<T> object.
Calls to query methods can be chained together in one query, which enables queries to become arbitrarily complex.
The following code example demonstrates how the standard query operators can be used to obtain information about a sequence.
string sentence = "the quick brown fox jumps over the lazy dog";
// Split the string into individual words to create a collection.
string[] words = sentence.Split(' ');
// Using query expression syntax.
var query = from word in words
group word.ToUpper() by word.Length into gr
orderby gr.Key
select new { Length = gr.Key, Words = gr };
// Using method-based query syntax.
var query2 = words.
GroupBy(w => w.Length, w => w.ToUpper()).
Select(g => new { Length = g.Key, Words = g }).
OrderBy(o => o.Length);
foreach (var obj in query)
{
Console.WriteLine("Words of length {0}:", obj.Length);
foreach (string word in obj.Words)
Console.WriteLine(word);
}
// This code example produces the following output:
//
// Words of length 3:
// THE
// FOX
// THE
// DOG
// Words of length 4:
// OVER
// LAZY
// Words of length 5:
// QUICK
// BROWN
// JUMPS
Query expression syntax
Some of the more frequently used standard query operators have dedicated C# and Visual Basic language keyword syntax that enables them to be called as part of a query expression. For more information about standard query operators that have dedicated keywords and their corresponding syntaxes, see Query Expression Syntax for Standard Query Operators (C#).
Extending the standard query operators
You can augment the set of standard query operators by creating domain-specific methods that are appropriate for your target domain or technology. You can also replace the standard query operators with your own implementations that provide additional services such as remote evaluation, query translation, and optimization. See AsEnumerable for an example.
Obtain a data source
In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from
clause comes first in order to introduce the data source (customers
) and the range variable (cust
).
//queryAllCustomers is an IEnumerable<Customer>
var queryAllCustomers = from cust in customers
select cust;
The range variable is like the iteration variable in a foreach
loop except that no actual iteration occurs in a query expression. When the query is executed, the range variable will serve as a reference to each successive element in customers
. Because the compiler can infer the type of cust
, you do not have to specify it explicitly. Additional range variables can be introduced by a let
clause. For more information, see let clause.
Note
For non-generic data sources such as ArrayList, the range variable must be explicitly typed. For more information, see How to query an ArrayList with LINQ (C#) and from clause.
Filtering
Probably the most common query operation is to apply a filter in the form of a Boolean expression. The filter causes the query to return only those elements for which the expression is true. The result is produced by using the where
clause. The filter in effect specifies which elements to exclude from the source sequence. In the following example, only those customers
who have an address in London are returned.
var queryLondonCustomers = from cust in customers
where cust.City == "London"
select cust;
You can use the familiar C# logical AND
and OR
operators to apply as many filter expressions as necessary in the where
clause. For example, to return only customers from "London" AND
whose name is "Devon" you would write the following code:
where cust.City == "London" && cust.Name == "Devon"
To return customers from London or Paris, you would write the following code:
where cust.City == "London" || cust.City == "Paris"
For more information, see where clause.
Ordering
Often it is convenient to sort the returned data. The orderby
clause will cause the elements in the returned sequence to be sorted according to the default comparer for the type being sorted. For example, the following query can be extended to sort the results based on the Name
property. Because Name
is a string, the default comparer performs an alphabetical sort from A to Z.
var queryLondonCustomers3 =
from cust in customers
where cust.City == "London"
orderby cust.Name ascending
select cust;
To order the results in reverse order, from Z to A, use the orderby…descending
clause.
For more information, see orderby clause.
Grouping
The group
clause enables you to group your results based on a key that you specify. For example you could specify that the results should be grouped by the City
so that all customers from London or Paris are in individual groups. In this case, cust.City
is the key.
// queryCustomersByCity is an IEnumerable<IGrouping<string, Customer>>
var queryCustomersByCity =
from cust in customers
group cust by cust.City;
// customerGroup is an IGrouping<string, Customer>
foreach (var customerGroup in queryCustomersByCity)
{
Console.WriteLine(customerGroup.Key);
foreach (Customer customer in customerGroup)
{
Console.WriteLine(" {0}", customer.Name);
}
}
When you end a query with a group
clause, your results take the form of a list of lists. Each element in the list is an object that has a Key
member and a list of elements that are grouped under that key. When you iterate over a query that produces a sequence of groups, you must use a nested foreach
loop. The outer loop iterates over each group, and the inner loop iterates over each group's members.
If you must refer to the results of a group operation, you can use the into
keyword to create an identifier that can be queried further. The following query returns only those groups that contain more than two customers:
// custQuery is an IEnumerable<IGrouping<string, Customer>>
var custQuery =
from cust in customers
group cust by cust.City into custGroup
where custGroup.Count() > 2
orderby custGroup.Key
select custGroup;
For more information, see group clause.
Joining
Join operations create associations between sequences that are not explicitly modeled in the data sources. For example you can perform a join to find all the customers and distributors who have the same location. In LINQ the join
clause always works against object collections instead of database tables directly.
var innerJoinQuery =
from cust in customers
join dist in distributors on cust.City equals dist.City
select new { CustomerName = cust.Name, DistributorName = dist.Name };
In LINQ, you do not have to use join
as often as you do in SQL, because foreign keys in LINQ are represented in the object model as properties that hold a collection of items. For example, a Customer
object contains a collection of Order
objects. Rather than performing a join, you access the orders by using dot notation:
from order in Customer.Orders...
For more information, see join clause.
Selecting (Projections)
The select
clause produces the results of the query and specifies the "shape" or type of each returned element. For example, you can specify whether your results will consist of complete Customer
objects, just one member, a subset of members, or some completely different result type based on a computation or new object creation. When the select
clause produces something other than a copy of the source element, the operation is called a projection. The use of projections to transform data is a powerful capability of LINQ query expressions. For more information, see Data Transformations with LINQ (C#) and select clause.
Query Expression Syntax Table
The following table lists the standard query operators that have equivalent query expression clauses.
See also
Feedback
Submit and view feedback for