Essential LINQ Operators

Part of the LINQ For Dummies Cheat Sheet

Language INtegrated Query (LINQ) uses an amazing array of operators. An operator is a special word that tells LINQ what kind of query to perform, such as checking whether two values are equal. Operators can run into the hundreds, but in most cases you'll perform the majority of tasks using only a few standard operators. Below is a list of essential LINQ operators.

Operator Description
from Defines the temporary object used to perform a query. Think of this operator as defining the individual element object from which you derive all other data for the query. For example, you choose what data will appear in the output and how that data is ordered based on the temporary variable. The from operator is the only one that must appear in every query you create.
in Defines the data source used to obtain the data. A data source can be any object supported by a data provider. The provider creates a connection between the data source and the LINQ query. For example, a data source can include an object such as a list box, an XML file, an SQL Server table, Active Directory, a Web service, or just about anything else you want to use to collect information. It's even possible to work directly with files on your hard drive using a LINQ query. A query may contain as many matching pairs of from and in operators as is needed to fully describe the data sources used in the query.
select Chooses the output information for the query. The simplest output is the same object you define using the from operator. However, a select operator can include calculated values, arrays of data, or individual object properties. The limiting factor in most cases is the functionality offered by the LINQ provider. All four of the default providers include the complete range of select operator behaviors. A query contains only one select operator and this operator must appear as the last operator in the query.
where Describes the conditions under which an element appears as part of the output. The where operator always requires a Boolean argument to define the output conditions. You can include as many where operators as needed to fully describe the output criteria. LINQ interprets the where operators in the order in which they appear in the query, so order is important. When a query doesn't work as anticipated, try changing the order of the where operators to better describe the output requirements. Use the debugger to help locate errors in the logical flow of where operators.
orderby Defines the order in which the output elements appear. The simplest orderby operator relies on the object you define using the from operator. However, an orderby operator can accept any statement that defines a logical ordering argument as input — normally numeric, alphabetical, symbol, or binary. You can use as many orderby operators as needed to fully describe the output order of the data. As with the where operator, LINQ interprets the orderby operator in the order in which it appears in the query. Consequently, the order of the orderby operators is important.
join and on Combines two or more data sources to create a composite view of the data. The data sources need not rely on the same providers. You can use as many join operators as required to fully describe all of the data sources used for a query. The on operator defines the criterion used to combine two data sources. Pair the join and on operators to create a complete data source.
let Creates a calculated value for use within the query. Calculated values make complex queries easier to formulate. In addition, you can pass a calculated value onto the caller to provide additional output information. In many cases, the let operator provides the means for passing data that LINQ won't ordinarily pass due to a naming conflict. You can use as many let operators as required to provide full query functionality, but you should limit the use of let to situations where a calculated value enhances query speed.
  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus

SERIES
LINQ For Dummies Cheat Sheet

Advertisement

Inside Dummies.com