Notes for learning from LINQ

Source: Internet
Author: User

Notes for learning from LINQ

Basic Concepts

As a breakthrough new feature launched by Microsoft, LINQ is Language integrated Query. It establishes a ing between objects and data, you can query data sets by accessing memory objects.

In. in the. NET class library, all the class libraries related to LINQ are in the System. in the Linq namespace, for the current program that needs to use LINQ to Object, the data source is a set of memory data that implements the interface IEumerable <T> or IQueryable <T>, for this program, that is, the List <T> set

The purpose of a LINQ query is to query data elements that meet specific conditions from a specified data source, and sort and connect the queried elements as needed, therefore, the LINQ query mainly includes the following elements:

Data source: the location from which data will be searched. It is usually one or more datasets. a dataset contains a series of elements and is an object of the IEumerable type <T> or IQueryable <T>, it can be enumerated to traverse every element. The element can be of any data type.

Target data: the data in the data source is not all the results required for query. The target data is used to specify the desired data. In LINQ, it defines the specific types of elements in the query result dataset.

Filtering condition: the filtering condition defines the filtering condition for elements in the data source. Only the original material that meets the conditions is returned as the query result. The handsome selection condition can be represented by a simple logical expression, it can also be expressed by functions with complex logic.

Additional operations: other operations, such as sorting, calculating the maximum value and sum of query results, and grouping query results.

Data Source and target data are essential elements, and filtering and additional operations are optional elements.

Query expression

The query expression is the basis of the LINQ query and the most commonly used method for compiling the LINQ query. An overall expression composed of query keywords and corresponding operands

Query expression keywords

Keywords

Function

From

Specifies the data source and range variable to be searched. Multiple from statements indicate that data is searched from multiple data sources.

Select

Specify the target data to be returned for query. You can specify any type or even anonymous type.

Where

Specify the filtering conditions for the elements. Multiple where clauses indicate the parallel conditions. Only when all the conditions are met can the elements be selected.

Orderby

Specify the sorting fields and sorting methods of the elements. When there are multiple sorting fields, the primary-secondary relationship is determined by the field order. You can specify two sorting methods: ascending and descending.

Group

Group field of the specified Element

Join

Associate multiple data sources

From

Each LINQ query starts from the from clause. It generally includes the following two functions:

Data sources will be used for formulating queries

Defines a local variable, indicating a single element in the data source

The format is from localVar in dataSource. Generally, the data type is not specified for the localVar element and is automatically assigned by the compiler.

Select

The LINQ query expression must end with a select or group clause, in the following format:

Select element

The element parameter specifies the element type and initialization method in the query result.

The target data to be selected can not only be an element in the data source, but also different operation results of the element, including attributes, methods, and operations.

You can use the anonymous type for further learning.

Where

The format is as follows:

Where expression

Expression is a logical expression. You can use & | to specify the logical operation Relationship between multiple conditions.

Orderby

The format is as follows:

Orderby element [sortType]

The default value is ascending.

Multiple sorting elements can be specified at the same time, or an independent sorting method can be set for each sorting element.

Group

Grouping the query results. The common format is as follows:

Group element by key

KeyIndicates grouping conditions,GroupThe return type of the clause isIGrouping <Tkey, TElement>The query result can be seen as an internal nestedListListHashtable,

If you want to sort the Group results and query the results again, you need to use the into keyword to save the Group query results to a temporary variable in the format

Group element by key into tmpGrp

Join

Through join clause join operations, you can associate elements that are not directly related to the same-source sequence in the object model. The only requirement is that each source needs to share a value that can be compared, to determine whether the values are equal

Three types of connections are available: internal connections, group connections, and left-right connections.

Internal Connection

The format is as follows:

Join element in dataSource on exp1 equals exp2

Exp1 and exp2 indicate two expressions with the same data type.

Group join

Join element in dataSource on exp1 equals exp2 into kgname

Into indicates that the data is grouped and saved to the maid.

Left Outer Join

LINQQuery Method

IEumerable <T>Interface

The main methods are as follows:

Numeric calculation: Sum, Average, Max, Min

Element Count, LongCount

Value: First, Last, ElementAt

Extracted subset: Skip, SkipWhile, Take, TakeWhile

Set Operations: ReverseAll, Concat, Except T, Intersect, Union, Distinct, SequenceEqual

Others: Any, Contains, ToArray, ToList...

LambdaExpression

Basic Format

(Input parameters) => expression

Parameters is a list of parameters. If there is only one input parameter in the Lambda expression, parentheses are not required. Otherwise, two or more parameters are separated by commas. If there is no parameter, empty parentheses are used to represent it.

Perform a series of operations using the corresponding Keyword Method

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.