Grouping is one of the most powerful capabilities of LINQ. The following examples show how to group data in various ways: By a single property. ![]() ![]() ![]() Query expression basics | Microsoft Docs. What is a query and what does it do? A 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. A query is distinct from the results that it produces. Generally, the source data is organized logically as a sequence of elements of the same kind. For example, a SQL database table contains a sequence of rows. In an XML file, there is a "sequence" of XML elements (although these are organized hierarchically in a tree structure). Grouping Data in a Microsoft Access Report: Creating a Report to Group Data by Age Ranges - this article explains how we can create a report that allows us to group. Oracle Migration Workbench Reference Guide for Microsoft Access 2.0, 95, 97, 2000 Migrations Release 9.2.0 for Microsoft Windows 98/2000 and Microsoft Windows NT. Technical articles, content and resources for IT Professionals working in Microsoft technologies. An in- memory collection contains a sequence of objects. From an application's viewpoint, the specific type and structure of the original source data is not important. The application always sees the source data as an IEnumerable< T> or IQueryable< T> collection. For example, in LINQ to XML, the source data is made visible as an IEnumerable< XElement>. Given this source sequence, a query may do one of three things: Retrieve a subset of the elements to produce a new sequence without modifying the individual elements. 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< int> high. Scores. Query =. from score in scores. Retrieve a sequence of elements as in the previous example but transform them to a new type of object. For example, a query may retrieve only the last names from certain customer records in a data source. 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. The following example shows a projection from an int to a string. Note the new type of high. Scores. Query. IEnumerable< string> high. Scores. Query. 2 =. String. Format("The score is {0}", score). Retrieve a singleton value about the source data, such as: The number of elements that match a certain condition. The element that has the greatest or least value. The first element that matches a condition, or the sum of particular values in a specified set of elements. For example, the following query returns the number of scores greater than 8. Score. Count =. (from score in scores. In the previous example, note the use of parentheses around the query expression before the call to the Count method. You can also express this by using a new variable to store the concrete result. This technique is more readable because it keeps the variable that stores the query separate from the query that stores a result. IEnumerable< int> high. Scores. Query. 3 =. Count = high. Scores. Query. 3. Count(). In the previous example, the query is executed in the call to Count, because Count must iterate over the results in order to determine the number of elements returned by high. Scores. Query. What is a query expression? A query expression is a query expressed in query syntax. A query expression is a first- class language construct. It is just like any other expression and can be used in any context in which a C# expression is valid. A query expression consists of a set of clauses written in a declarative syntax similar to SQL or XQuery. Each clause in turn contains one or more C# expressions, and these expressions may themselves be either a query expression or contain a query expression. A query expression must begin with a from clause and must end with a select or group clause. Between the first from clause and the last select or group clause, it can contain one or more of these optional clauses: where, orderby, join, let and even additional from clauses. You can also use the into keyword to enable the result of a join or group clause to serve as the source for additional query clauses in the same query expression. Query variable In LINQ, a query variable is any variable that stores a query instead of the results of a query. More specifically, a query variable is always an enumerable type that will produce a sequence of elements when it is iterated over in a foreach statement or a direct call to its IEnumerator. Move. Next method. The following code example shows a simple query expression with one data source, one filtering clause, one ordering clause, and no transformation of the source elements. The select clause ends the query. Main(). // Data source. Query Expression. IEnumerable< int> score. Query = //query variable. Execute the query to produce the results. Score in score. Query). Console. Write. Line(test. Score). }. // Outputs: 9. In the previous example, score. Query is a query variable, which is sometimes referred to as just a query. The query variable stores no actual result data, which is produced in the foreach loop. And when the foreach statement executes, the query results are not returned through the query variable score. Query. Rather, they are returned through the iteration variable test. Score. The score. Query variable can be iterated in a second foreach loop. It will produce the same results as long as neither it nor the data source has been modified. A query variable may store a query that is expressed in query syntax or method syntax, or a combination of the two. In the following examples, both query. Major. Cities and query. Major. Cities. 2 are query variables: //Query syntax. IEnumerable< City> query. Major. Cities =. from city in cities. Population > 1. Method- based syntax. IEnumerable< City> query. Major. Cities. 2 = cities. Where(c => c. Population > 1. On the other hand, the following two examples show variables that are not query variables even through each is initialized with a query. They are not query variables because they store results: int highest. Score =. (from score in scores. IEnumerable< int> score. Query =. from score in scores. Score = score. Query. Max(). // the following returns the same result. Score = scores. Max(). List< City> large. Cities. List =. (from country in countries. Cities. where city. Population > 1. To. List(). // or split the expression. IEnumerable< City> large. Cities. Query =. from country in countries. Cities. where city. Population > 1. List< City> large. Cities. List. 2 = large. Cities. Query. To. List(). For more information about the different ways to express queries, see Query syntax and method syntax in LINQ. Explicit and implicit typing of query variables This documentation usually provides the explicit type of the query variable in order to show the type relationship between the query variable and the select clause. However, you can also use the var keyword to instruct the compiler to infer the type of a query variable (or any other local variable) at compile time. For example, the query example that was shown previously in this topic can also be expressed by using implicit typing: // Use of var is optional here and in all queries. Cities is an IEnumerable< City> just as. Cities =. from city in cities. Population > 1. For more information, see Implicitly typed local variables and Type relationships in LINQ query operations. Starting a query expression A query expression must begin with a from clause. It specifies a data source together with a range variable. The range variable represents each successive element in the source sequence as the source sequence is being traversed. The range variable is strongly typed based on the type of elements in the data source. In the following example, because countries is an array of Country objects, the range variable is also typed as Country. Because the range variable is strongly typed, you can use the dot operator to access any available members of the type. IEnumerable< Country> country. Area. Query =. from country in countries. Area > 5. 00. 00. The range variable is in scope until the query is exited either with a semicolon or with a continuation clause. A query expression may contain multiple from clauses. Use additional from clauses when each element in the source sequence is itself a collection or contains a collection. For example, assume that you have a collection of Country objects, each of which contains a collection of City objects named Cities. To query the City objects in each Country, use two from clauses as shown here: IEnumerable< City> city. Query =. from country in countries. Cities. where city. Population > 1. For more information, see from clause. Ending a query expression A query expression must end with either a group clause or a select clause.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2017
Categories |