In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to analyze the LINQ framework in depth by. Net. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.
One: LINQ executes the expression
In the process of studying LINQ, I have referred to a lot of technical articles and technical books. There is no doubt that the call entry of Linq to Provider is to parse Lambda expressions into Expression expression objects. Unlike Linq to Object, Linq to Object is a delegate that parses Lambda directly into generic Func types, but many of us, including myself, have ignored a big detail, that is, Provider will execute Expression internally. It is not the way we understand that the expression Expression object is fully parsed into the equivalent SQL, that is to say, Expression is not as simple as we see, it has dual context logic in it.
We all directly use LINQ as the query interface, and VS is responsible for parsing the syntax of LINQ and translating it into corresponding extension method calls during the final compilation. One important aspect we ignore is that VS executes LINQ expressions when parsing and translating LINQ. This is very important. I always thought that VS was only responsible for translating LINQ expressions into equivalent extension method calls, but later I found that VS allows us to write LINQ statements with logical judgment expressions in order to meet our need to concatenate Where sentences when we are unable to determine the object conditions. This function is very convenient for us to make multi-conditional combination queries, without the need for multiple judgments of IF and ELSE. Just let nature take its course and make the judgment in the first expression in LINQ. Comrades in pursuit of elegant code do not want to use two query methods in a method with both LINQ query and chain query. It would be perfect if LINQ can satisfy most of the query functions. [copyright Wang Qingpei, please give your signature when reprinted]
To show that LINQ will be executed by VS at compile time, let's take a look at it with the LINQPad tool
LINQ query expression: from truck in TB_CX_TRUCKs where 1 query 1 select truck
LINQ equivalent chain method: TB_CX_TRUCKs.Where (truck = > True)
Figure 1:
If there is no execution, it is reasonable to parse directly into the format of Lambda (truck) = > 1 to resolve 1, and then let the LINQ to Provider provider handle it, it may feel that there is no real meaning anyway, so the expression is parsed like this. Let's try to write it in a different way. [copyright Wang Qingpei, please give your signature for reprint]
LINQ query expression: from truck in TB_CX_TRUCKs where string.IsNullOrEmpty ("1111") select truck
LINQ equivalent chain method: TB_CX_TRUCKs.Where (truck = > String.IsNullOrEmpty ("1111"))
Figure 2:
It can be concluded that LINQ statements are two actions that will be executed and parsed, and before entering the provider, we can see that LINQ can be accompanied by some execution logic, rather than the final SQL execution logic.
The processing of expressions can be divided into constant expressions and dynamic variable expressions. Constant expressions can directly evaluate whether the expression is true or false when VS compiles. On the other hand, dynamic variable expressions need to be evaluated later in expression parsing, in other words, the Provider provider in Linq to Provider is an expression executor with high intelligence, not only equivalent parsing of expressions, but also custom logic code for parsing expressions. [copyright Wang Qingpei, please give your signature for reprint]
For example, we all have the experience of stitching query conditions. There are N query condition fields on the interface, which need to be dynamically stitched into the LINQ statement according to whether the user has filled in which field. Generally speaking, we will make the judgment of if, because we all think that the conditional expression after Where is directly parsed into the SQL statement of the corresponding logic, so as long as it is spliced into the Where clause of SQL. Because LINQ cannot be split and assembled, it must be written at once before it can be compiled. So we are all using query expansion method for data query, this dilemma so that we can not see the elegance of LINQ, but has not been used.
By observing the SQL statements parsed by the LINQPad tool, it is found that the LINQ query expression will be executed and parsed within the provider, which is the same as the VS process, which can be executed first and then parsed, and the parsing is based on the previous execution. Let's take a look at a relatively simple SQL and chained method after LINQ parsing. [Wang Qingpei all rights reserved, please give your signature for reprint]
LINQ query expression: from truck in TB_CX_TRUCKs where 1 query 1 | | truck.LICENSE_NUMBER.Length (True | | (truck.LICENSE_NUMBER.Length)
< 10))) 图3: 对照链式方法,很明显VS先对1==1表达式进行了执行并返回true作为后面整个表达式的一部分拼接进Where链式方法,所以先执行再解析两个过程。然后我们对最后的SQL进行分析,没有看见任何Where语句,为什么呢?是因为提供程序在内部对表达式进行了执行并分析了我们想要的输出结果,也不知道这样的效果是不是为了满足我们多条件拼接的问题。 由于Where方法里面的Lambda表达如果被执行的话,那么将不会执行(truck.LICENSE-NUMBER.Length truck.LICENSE_NUMBER.Contains(truckModel.LICENSE_NUMBER)); if (!string.IsNullOrEmpty(truckModel.TRUCK_MODEL_CODE)) queryList = queryList.Where(truck =>Truck.TRUCK_MODEL_CODE.Contains (truckModel.TRUCK_MODEL_CODE))
If there are many query conditions, then we will have to write a lot of such judgment code, which is neither convenient nor beautiful. [copyright Wang Qingpei, please give your signature for reprint]
OR query between multiple conditions
Although in many cases we use the where keyword in Linq to piece together query conditions, there is a requirement that Linq query can not satisfy us, that is, the relationship between multiple conditions is OR. Because as long as the where condition in the SQL statement written by Linq or chained method will be followed by and relation, we can only use chained method to split.
Public List GetList (DutyModel dutyModel)
{
Using (UserOrgDemo2Entities Context = new UserOrgDemo2Entities ())
{
IQueryable result = Context.TB_DUTY.AsQueryable ()
System.Linq.Expressions.Expression expressionDUTY = DynamicLinqExpressions.True ()
If (! (dutyModel.RANK_NO = = 0))
ExpressionDUTY.Or (duty = > duty.RANK_NO = = dutyModel.RANK_NO)
If (! string.IsNullOrEmpty (dutyModel.USER_PRIV))
ExpressionDUTY.Or (duty = > duty.USER_PRIV = = dutyModel.USER_PRIV)
Return result.Where (expressionDUTY) .Select (tb_duty= > new DutyModel () {USER_PRIV=tb_duty.USER_PRIV}) .ToList ()
}
}
A key point here is that foreigners (probably more powerful predecessors, thank you here!) Write a * .cs file, which is the expansion method of Expression expression file, which is mainly used for multi-conditional Or, And combination query.
It is said that if the and relationship between multi-conditional combination queries can be directly used Linq, if it is or together with and, then you can use the above chain query method.
In fact, said that there is only one purpose, LINQ parsing process is not only a "provider translation into SQL" process, but includes two stages, four processes of processing, LINQ writing in many ways, the principle should be similar, as long as we write LINQ comprehensive consideration of these processes, should be very helpful for us to deal with complex queries.
Net on how to in-depth analysis of the LINQ framework to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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: 263
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.