LINQ: start to use LINQ (2)-Basic LINQ query operations, linq Query

Source: Internet
Author: User

LINQ: start to use LINQ (2)-Basic LINQ query operations, linq Query
Getting started with LINQ (2)-Basic LINQ query operations

The first step is to specify the data source. Like in most programming languages, variables must be declared in C # before they can be used. In the LINQ queryFromThe purpose of a clause is to introduce the data source (MERS mers) and range variable (cust ).

1 // queryAllCustomers is IEnumerable <Cutsomer> type 2 var queryAllCustomers = from cust in customers3 select cust;

The range variable is similarForeachIteration variables in the loop, but in the query expression, there is actually no iteration. When a query is executed, the range variable is used as a reference to each subsequent element in mers MERs. Because the compiler can infer the cust type, you do not need to explicitly specify this type.

 

Ii. filter: where

Perhaps the most common query operation is to apply a filter in the form of a Boolean expression. This filter allows the query to return only the elements whose expression result is true. UseWhereClause to generate results. In fact, the filter specifies which elements are excluded from the source sequence.

In the following example, only those addresses in London are returned.

1 var queryLondonCustomers = from cust in customers2                       where cust.City = "London"3                       select cust;

  

You can use the familiar C # Logic AND (&&)And OR (|)Operators WhereClause to apply any number of filter expressions. For example, to return only customers in "London" and named "Devon:
1 where cust.City = "London" && cust.Name = "Devon"

To return customers located in London or Paris:

1 where cust.City = "London" && cust.Name = "Paris"

 

Iii. Sorting: orderby can sort the returned data conveniently. OrderbyThe clause sorts the elements in the returned sequence according to the default comparator of the sorted type. For example, the following query can be extended to sort the results by Name attribute. Because Name is A string, the default comparator sorts the letters from A to Z.
1 var queryLondonCustomers = from cust in customers2                 where cust.City = "London"3                 orderby cust.Name descending 4                 select cust;

To sort the results in reverse order (from Z to A), use orderby... Descending clause.

4. group: group

UseGroupClause, You can group results by the specified key. For example, you can specify that results should be grouped by City so that all customers in London or Paris are in their respective groups. In this example, cust. City is the key.

 1 var queryLondonCustomers = from cust in customers 2                 group cust by cust.City; 3  4 foreach (var queryLondonCustomer in queryLondonCustomers) 5 { 6    Console.WriteLine(queryLondonCustomer.Key); 7    foreach (var cust in queryLondonCustomer) 8    { 9       Console.WriteLine(cust.Name);10    }11 }

In useGroupWhen the clause ends the query, the result is in the list form. Each element in the list is an object with a Key member and a list of elements grouped by the Key. You must use nestedForeachLoop. The External Loop is used to access each group cyclically, and the internal loop is used to access members of each group cyclically.

If you must reference the results of the group operation, you can useIntoKeyword to create an identifier that can be further queried. The following query returns only groups of customers with more than two types:

1 // custQuery is IEnumable <IGrouping <string, Customer> type 2 var custQuery = from cust in customers3 group cust by cust. city4 into custGroup5 where custGroup. count ()> 26 orderby custGroup. key7 select custGroup;

 

V. join: join

There is no explicitly modeled sequence Association in the data source created by the join operation. For example, you can perform connections to find all customers and dealers located in the same location. In LINQ,JoinThe clause always runs on an object set instead of a database table.

1 var innerJoinQuery = from cust in customers2                   join dist in distributors on cust.City equals dist.City3                   select new {CustomerName = cust.Name, DistributorName = dist.Name};

In LINQ, you do not need to use it as frequently as in SQL.Join, Because the foreign key in LINQ is represented as an attribute that contains the item set in the object model. For example, a Customer object contains a set of Order objects. You do not need to perform the join operation. You only need to use the dot notation to access the order:

1 from order in Customer.Orders...

 

6. Projection: select

  SelectClause to generate query results and specify the "shape" or type of each returned element. For example, you can specify whether the result contains the entire Customer object, only one member, a subset of the members, or a completely different result type created based on the calculation or new object. WhenSelectWhen the sub-statement generates content other than the Source Element copy, this operation is called "projection ".

 

[Source] This article mainly comes from Microsoft official documentation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.