standard linq query operators


The standard LINQ query operators are divided into two categories.

Lazy operators
Greedy operators


The operators that defer the query execution are called the Lazy operators. The operators that executes the query immediately are called the Greedy operators.

Example of Lazy operators are:

  • From
  • Where
  • Select
  • SelectMany
  • Join
If we construct a query using above operators, the query will not be executed immediately and will be deferred. We can iterate and retrieve the query results. The following is a typical query using lazy operators. The below query does not retrieve until you iterate in query results using for each loop. It is called deferred evaluation

var queryByCountry =
      from cust in customers
      where cust.Age < 20 
      select cust;
foreach (var c in queryByCountry)
{      
	Console.WriteLine(c.CustomerID);       
}

Example of Greedy operators

  • ToList
  • ToArray
  • Todictionary
  • ToLookup

The following a is typical query using Greedy operators.

SalesDBDataContext dataContext = new SalesDBDataContext();
var custQuery =
				from c in dataContext.Customers
				select c;
List qList = custQuery.ToList();

In the above code, the ToList() operator tells LINQ to evaluate the query immediately.

Lazy operators can further be categorized as

  • streaming operators
  • non-streaming operators
streaming operators
A streaming operator works on a query sequence and does not look at all the data of a query.

Examples of streaming operators.

  • Distinct
  • Except
  • Select
  • SelectMany
  • Skip
non-streaming operators

A frequently used examples of a non streaming operator is OrderBy. The OrderBy operator will traverse all the data and finds the first element in the query result.

The standard query operators are defined as extension methods and can be used to work with different data sources, in-memory objects or databases or Xml documents. These operators facilitates the manipulation of data sources efficiently.

All the standard operators are classified into the following categories based on their operation.

  • Filtering Operator
  • Projection Operator
  • Sorting Operator
  • Join Operator
  • Grouping Operator
  • Quantifier Operator
  • Set Operator