LINQ series (1) -- start

Source: Internet
Author: User

Hello, everyone. I am very happy to share with you some experiences in my limited development experience in the blog Park. First of all, I would like to share with you some of my understanding of LINQ, most of the content in this series is based on my own understanding of the original understanding and elaboration of some issues, the so-called original, I hope that if you find anything wrong during your reading, you will be able to point it out in a timely manner. This is also a great help and Improvement for me. You are also very welcome to repost this series.ArticleBut I hope to indicate the source when I reprint it. Thank you.

Before writing specific content, first share my reference in this section-msdn.

Http://msdn.microsoft.com/zh-cn/library/bb397676.aspx

Http://msdn.microsoft.com/zh-cn/library/bb397906.aspx

Http://msdn.microsoft.com/zh-cn/library/bb397933.aspx

Http://msdn.microsoft.com/zh-cn/library/bb397896.aspx

First of all, let's talk about the nonsense that everyone may know. What is LINQ used.

That isLanguage integration Query(LINQ ),Is the name of a group of technologies.

LINQSupported data sources:SQLDatabases,Ado. netDataset,XMLDocument and stream and. NetSet.

As mentioned on msdn, all LINQ query operations are composed of the following three different operations:

1. Obtain the data source.

2. Create a query.

3. Execute the query.

First, we analyze Step 2 and Step 3. The process of creating a query and executing a query is two separate processes, and all the LINQ statements are delayed.

Next, let's take a look at the first step to obtain the data source.SetCombined, ADO. netDataset) And remote data sources (SQLDatabase, XMLDocuments and streams.

Let's clarify what kind of data source can be processed by LINQ. OK. To understand this problem, let's take a look at Step 3 -- execute the query. How is LINQ executed? Let's look at an example:

Dataclasses1datacontext datacontext = new dataclasses1datacontext ();

String AA = "Wuhan ";
VaR query = from item in datacontext. MERs
Where item. City = AA & item. customerid = 1
Select item;

Foreach (customer in query. toarray ())
{
Console. writeline ("customerid: {0} city: {1}", customer. customerid, customer. City );
}

 

As you can see in the example, I believe everyone understands that the query in LINQ is executed in foreach, so is it only when youWriteIn foreach modeWriteIf foreach is used, LINQ will not be executed, and the answer is obviously no. If you use aggregate functions such as query. first (), query. max () and LINQ will also be executed. However, although you didn't write foreach at this time, the execution principle of Aggregate functions still uses foreach.

This leads to a necessary condition for the data source that can execute LINQ -- to be able to be foreach. Then, what data source can be foreach, implementing the ienumerable data source interface.

Of course, the above write is only fromDeduce a"Necessary Conditions"To help you understand and remember why the data source supported by LINQ must implement the ienumerable interface, does it implement the ienumerable interface? Let's analyze this problem.

On msdn:
By using the declarative query syntax introduced in C #3.0, most queries in the introductory LINQ document are written as Query expressions. However, the. NET public Language Runtime Library (CLR) itself does not have the concept of query syntax. Therefore, during compilation, the query expression is converted to the content that the CLR does understand: method call. These methods are called "standard query operators" and have the following names:Where,Select,Groupby,Join,Max,Average. You can call these methods directly by using method syntax instead of query syntax.

Let's take a look at the msdn example:

Int[] Numbers = {5, 10, 8, 3, 6, 12 };

// Method Syntax:
Ienumerable <Int> Numquery2 = numbers. Where (num => num % 2 = 0). orderby (n => N );
Okay. Let's take a look at where these extension methods are defined and which class they are extended.

These extension methods are defined in

ProgramSet:System. Core(In system. Core. dll)

Namespace:System. LINQ

Class: Enumerable

Medium,

Provides a set of query implementations.Ienumerable<T>ObjectStatic(In Visual BasicShared) Method.

OK. Now let's take a look at the knowledge. The query expression is executed in the CLR through the extension method, and the extension method is implemented.Ienumerable<T>ObjectStaticMethod.

So here, we fully understand why the data source implementation of LINQIenumerableThis interface is not enough and must be implementedIenumerable <t>Interface data source.

(Inheritance structure: public interface ienumerable <t>: ienumerable ).

If you want to use LINQ, you must introduce the namespace system. LINQ, because the extension method is defined in the enumerable class of the namespace.

Please note that what I personally think is important is not to remember the conclusion we finally launched, but to understand the process of this derivation and to clarify the knowledge points used in the derivation, use this derivation process to link these knowledge points.

Finally, we will provide an introduction to the next section: In. net, you can use the local data source of LINQ to implementIenumerable <t>This interface. The Remote Data Source implementsIqueryable <t>This interface.

(Inheritance structure: public interface iqueryable <t>: ienumerable <t>, iqueryable, ienumerable ).

Finally, I would like to thank Cai Peng for his constant help. I hope you will continue to pay attention to my blog and add his blog link:
Http://travelcai.cnblogs.com/

 


 

 

 

 

 

 

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.