How to use linq dynamic conditional queries
This article mainly explains how to use linq dynamic condition query. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use linq dynamic condition query.
1Construction of expression tree for dynamic conditions of LINQ
Private Expression getCondition ()
{
Expression expression = blog = > true
If (! String.IsNullOrEmpty (Request ["BlogClassID"]))
{
Int blogClassID
If (Int32.TryParse (Request ["BlogClassID"], out blogClassID))
{
Expression e2 = blog = >
Blog.BlogClass = = null
Var invokedExpr = Expression.Invoke
(e, expression.Parameters.Cast
())
Expression = Expression.Lambda
(Expression.And (expression.Body, invokedExpr), expression.Parameters)
}
}
Return expression
}
The main query looks like this:
Var result = new DongBlogDataContext () .Blogs.Where (getCondition ()
Because according to SQL tracking, generating SQL is similar:
SELECT [t0]. [BlogID], [t0]. [ChannelID], [t0]. [BlogClassID], [t0]. [Title], [t0]. [Content], [t0]. [Tag], [t0]. [CreateDateTime] FROM [dbo]. [Blog] AS [t0] WHERE [t0]. [BlogClassID] IS NULL
The essence of this method is to merge Lamba expressions, that is, these three sentences.
SELECT [t0]. [BlogID], [t0]. [ChannelID], [t0]. [BlogClassID], [t0]. [Title], [t0]. [Content], [t0]. [Tag], [t0]. [CreateDateTime] FROM [dbo]. [Blog] AS [t0] WHERE [t0]. [BlogClassID] IS NULL
It would be troublesome to write this for every conditional merge, but fortunately someone has already written a helper class:
Using System; using System.Linq; using System.Linq.Expressions; using System.Collections.Generic; public static class PredicateBuilder {public static Expression True () {return f = > true;} public static Expression False () {return f = > false;} public static Expression Or (this Expression expr1, Expression expr2) {var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast ()) Return Expression.Lambda (Expression.Or (expr1.Body, invokedExpr), expr1.Parameters);} public static Expression And (this Expression expr1, Expression expr2) {var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast ()); return Expression.Lambda (Expression.And (expr1.Body, invokedExpr), expr1.Parameters) }}
This class can be used for merging expressions of type Expression.
2 the construction of Query for the dynamic condition of LINQ
It is better to write the same query:
Private IQueryable getQuery () {IQueryable query = new DongBlogDataContext (). Blogs; if (! String.IsNullOrEmpty (Request ["BlogClassID"])) {int blogClassID; if (Int32.TryParse (Request ["BlogClassID"], out blogClassID)) query = query.Where (blog = > blog.BlogClass = = null);} return query.OrderByDescending (blog = > blog.CreateDateTime) }
Main query
Var result = getQuery ()
The generated SQL and * * are exactly the same.
At this point, I believe you have a deeper understanding of "how to use linq dynamic condition query". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!