Data Operations So easy-LINQ parsing, soeasy-linq

Source: Internet
Author: User

Data Operations So easy-LINQ parsing, soeasy-linq

1. What is LINQ?

The abbreviation of Language Integrated Query is "Language Integrated Query. To provide a unified query method that spans various data sources, the proposed method mainly includes four components: LINQ to Objects, Linq to XML, Linq to DataSet, and Linq to SQL.

The following is a brief introduction to the four components of LINQ.

Linq to SQL component: It can query data based on relational databases. Microsoft only queries the SQL Server database, including querying, modifying, and deleting the data.

Linq to DataSet component: It can query data in the DataSet object, and can add, delete, modify, and query data.

Linq to XML component: this component can query XML files. Before it is proposed, C # uses XPath to query XML files. However, this tool is not as concise as Linq to XML.

Linq to Object component: this component can query collection data, such as arrays or lists.

 

2. Where is LINQ good?

Good thing: LINQ makes operations on data sources easier, easier, and easier to understand.

2.1 query expression

Before explaining the benefits of LINQ, you must first understand the "query expression ". The query expression must start with the from clause and end with the select or group clause. Between the first from clause and the last select or group clause, it can contain one or more where clauses, orderby clauses, and join clauses. The format is very similar to the SQL language, but it is written backwards.

// Query expression var queryExp = from s in collection

Select s;

// There is another expression for the query expression, that is, the "dot tag mode", var queryExp = collection. Select (s => s );

As for how to choose, it depends on the individual habits of programmers.

2.2 Use Linq to Objects to query a set

Prior to the proposal of LINQ, for and foreach are generally used to query the set. However, this method is not as concise as that of LINQ to Objects, and it is not easy to add filtering conditions.

// Create a query expression to obtain the element var queryResults = from item in collection where item % 2 = 0 select item;

From the code above, we can see that the code is more concise by using Linq to Objects to query collection Objects. In addition, you can add filtering conditions in the where clause.

 

2.3 query XML files using Linq to XML

// Create a query and obtain the element var queryResults = from element in xmlDoc. elements ("Person") where element. element ("Name "). value = "Li Si" select element;

From the code above, we can see that the code for querying XML files using Linq to XML is much simpler and requires only one query expression.

In this example, we will not write any data here. The usage is similar to that of Linq to Object, but that of the data set Object instead of the data set Object.

 

3. Understand the essence of LINQ

In fact, C #3.0 does not add any new features, because for the compiler, the code for the new features of C #3.0 is the same as the previous code, which is no exception to LINQ. We can draw from the IL code that the LINQ expression is based on the Lambda expression and the extension method, while the Lambda expression is based on the delegate, and the extension method is also a method, therefore, the essence of LINQ is to call methods.

 

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.