
For example, the following query returns the number of scores greater than 80 from the scores integer array: int highScoreCount = The first element that matches a condition, or the sum of particular values in a specified set of elements. The element that has the greatest or least value. The number of elements that match a certain condition. Retrieve a singleton value about the source data, such as: The following example shows a projection from an int to a string. Or it may retrieve the complete record and then use it to construct another in-memory object type or even XML data before generating the final result sequence. For example, a query may retrieve only the last names from certain customer records in a data source. Retrieve a sequence of elements as in the previous example but transform them to a new type of object.

The query may then sort or group the returned sequence in various ways, as shown in the following example (assume scores is an int): IEnumerable highScoresQuery = Retrieve a subset of the elements to produce a new sequence without modifying the individual elements.

Given this source sequence, a query may do one of three things: For example, in LINQ to XML, the source data is made visible as an IEnumerable. The application always sees the source data as an IEnumerable or IQueryable collection.

An in-memory collection contains a sequence of objects.įrom an application's viewpoint, the specific type and structure of the original source data is not important. In an XML file, there is a "sequence" of XML elements (although these are organized hierarchically in a tree structure). For example, a SQL database table contains a sequence of rows. Generally, the source data is organized logically as a sequence of elements of the same kind. A query is distinct from the results that it produces. What is a query and what does it do?Ī query is a set of instructions that describes what data to retrieve from a given data source (or sources) and what shape and organization the returned data should have. This article introduces the basic concepts related to query expressions in C#.
