Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the overall framework of Linq?

2025-02-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how the overall framework of Linq is, the article is very detailed, has a certain reference value, interested friends must read it!

LINQ, language integrated query, is to integrate some query operations into the language (seemingly nonsense), such as querying relational databases, and provide a consistent mode of operation, no matter where the final data is stored? In memory, the remote database is also a Xml file storage, not only that, you can also use your rich imagination to expand your own query. Linq to SQL undoubtedly brings the energy of Linq into full play, so let's take a look at the overall framework of Linq in terms of the Linq to SQL architecture.

In the last two chapters, we discussed the initialization of DataContext and the acquisition of Table objects, as well as the initialization of Provider through the source code. Today, let's take a look at the large-scale processes implemented by Linq to SQL.

If we write code like this:

DataContext dbCtx = new DataContext ("server=localhost;database=cnblogs;user id=sa;pwd=sa"); Table posts = dbCtx.GetTable (); foreach (Post p in posts) {Console.WriteLine (p.Title);}

What happened behind the scenes?

When you look at the foreach code, you must know that this Table must implement the IEnumerable interface. The foreach code here has the same effect as the following code. In fact, it is transformed into this code in the end:

IEnumerator iterator = posts.GetEnumerator (); while (iterator.MoveNext ()) {Post p = iterator.Current; Console.WriteLine (p.Title);}

The following expression is actually converted to an anonymous method when the expression is compiled, and the IsTrue is a "pointer to the method", no different from the delegate we are already familiar with. When the second formula is executed, the expression on the right will be compiled into the data structure of a tree. (the C # compiler actually does lexical analysis and syntax analysis for us. In the compilation principle, the first two stages are lexical analysis and parsing. Lexical analysis first traverses the incoming language string, in our case, x = > xanalyzer 5. The lexical analyzer reads each character and identifies identifiers, constants and keywords. Operator, the output of the lexical analyzer is Token (symbol) Then the parser organizes the Token into a tree structure according to the syntax paradigm of the language, and uses this tree structure to represent the language's code files. In our case, x = > x expressions 5 is the statement of the "language" of Lambda expressions, and Expression is the tree. Expression is a recursive form of definition, it has two properties: Parameters, this property is the parameter of the Lambda expression, in the above code is: X, it also has a property is Body,Body is also an Expression type, from here we can see that Expression is recursive.

As described above, the Lambda expressions accepted by the extension methods in Queryable are actually compiled into tree data structures. These tree data structures carry the syntax of query expressions, but in the end what kind of data do they query, which is the database? Or XML or Web Service will have to be parsed by IQueryProvider, from which we can probably see a structure like this:

Seeing this picture, how many ways do we have to extend Linq?

*: by adding extension methods to IEnumerable or IQueryable, this is achieved by making use of the features of the C # language. I call this expansion horizontal expansion.

The second is to implement IQueryProvider on your own, which is extended vertically, providing your own Provider, and then the Provider parsing expression tree generates the final specific query operation.

The above is all the content of this article "what is the overall framework of Linq?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report