1.3 QueriesM extends LINQ query comprehensions with several features to make authoring simple queries more concise. The keywords, where and select are available as binary in.x operators. Also, indexers are automatically added to strongly typed collections. These features allow common queries to be authored more compactly as illustrated next.1.3.1 FilteringFiltering extracts elements from an existing collection. Consider the following collection:People {{ First = "Mary", Last = "Smith", Age = 24 },{ First = "John", Last = "Doe", Age = 32 },{ First = "Dave", Last = "Smith", Age = 32 },}
This query extracts people with Age == 32 from the People collection:from p in Peoplewhere p.Age == 32select pAn equivalent query can be written with either of the following expressions:People where value.Age == 32People.Age(32)The where operator takes a collection on the left and a Logical expression on the right. The where operator introduces a keyword identi.er value into the scope of the Logical expression that is bound to each member of the collection. The resulting collection contains the members for which the expression is true. The expression:Collection where Expressionis exactly equivalent to:from value in Collectionwhere Expressionselect valueCollection types gain indexer members that correspond to the .elds of their corresponding element type. That is, this:Collection . Field ( Expression )is equivalent to:from value in Collectionwhere Field == Expressionselect value1.3.2 SelectionSelect is also available as an in.x operator. Consider the following simple query:from p in Peopleselect p.First + p.LastThis computes the select expression over each member of the collection and returns the result. Using the in.x select it can be written equivalently as:People select value.First + value.LastThe select operator takes a collection on the left and an arbitrary expression on the right. As with where, select introduces the keyword identi.er value that ranges over each element in the collection. The select operator maps the expression over each element in the collection and returns the result. The expression:Collection select ExpressionIs exactly equivalent to:from value in Collectionselect ExpressionA trivial use of the select operator is to extract a single .eld:People select value.FirstCollections are augmented with accessors to .elds that can be extracted directly. For example People.First yields a new collection containing all the .rst names, and People.Last yields a collection with all the last names.