Share the components and principles of the dynamic concatenation Expression, expression expressions
Preface
As we all know, it is also good to use, but there is a problem. When you use LINQ for search, you write it like this.
OK, it looks good, but if you want to perform a dynamic search... haha! In fact, there are still a lot of methods, but it's just a big bend.
What is dynamic search? By the way, if you create a table page with three columns: user name, registration time, and level, you want to implement dynamic combined search, the user name column is added to the search condition only when the user specifies to search by user name. The traditional SQL statement does this.
Statement: only use examples. Running is not important! (The above code must be problematic in general.)
So can you do this with linq? Er... to the following
Yes, yes, but in actual projects, the IQueryable interface is not directly returned, so there is no way to do this.
Therefore, the dynamic concatenation of linq came into being. The essence of linq is the Expression tree. You can see that the signature of the Where extension method of IQueryable starts with Expression.
Introduction and use of components
The initial purpose of encapsulation is also for your own use. I am a lazy, so the simpler I will make the components, the better. The following is a complete example.
The above code is spliced to the expression tree.
The annotations are clear, but the Build method prototype of the 2nd and 10th rows needs to be explained in particular.
The returned value is of the Expression <Func <TParameter, bool> type. There is a generic parameter, which is the User passed during the instantiation of row 2nd, you must ensure that the attributes of each search condition you add are included in the User class.
Why is it designed like this? This feeling is unclear at once. Let's talk about the principle later.
In addition, we can see that there is a filterNameMap parameter, which is mainly used to convert attribute names and is generally used for foreign keys. Let me briefly describe my design intent.
For example, there is a list showing the role information in the permission system, including the role name, description, and features. The third column is from the menu, others are from role tables.
In general, the attribute name in the third column may be set to Privileges, and the type is string. But if you want to search by role function, you cannot filter by string? It is usually filtered by the function Id, that is, PrivilegeId.
Because I use ExtJs (skip this section without using this section), the problem arises. The parameter name that extjs sends to me is Privileges, and the value is a set, because my Model class property name is this, but the real attribute used for filtering in my background is PrivilegeId, so I need to map Privileges to PrivilegeId and tell ExpressionBuilder, if you encounter a Search Condition for the Privileges attribute name, replace the attribute name with PrivilegeId for splicing.
For ease of understanding, the following is the source code of SqlFilter, which is very simple.
Below is Operation's
Component Principle
I'm not sure I can make it clear ···
It is actually the principle of splicing expression trees.
This is a rather simple expression tree, and the annotations are clearly written. The following is a complex
As you can see, the first sentence is of the User type, so I used the generic type in the design of ExpressionBuilder to input this parameter.
Let's take a look at the source code for more examples.
Postscript
Continue advertising · please contact QQ Group 74522853 to learn more about related technologies.