linq to sql how to solve the problem of multi conditional query the answer using expression tree! Under the of

  • 2020-05-12 02:30:45
  • OfStack

How to achieve lazy loading in the real sense, that is, only extract the part of data we need from the database at one time. Through the research, we have the following methods:
First of all, we will create a new static class to store various combinations of multi-conditional combination queries, such as or,And, etc. The code is as follows:
 
using System.Linq.Expressions; 

public static class PredicateExtensionses 
{ 
public static Expression<Func<T, bool>> True<T>() { return f => true; } 

public static Expression<Func<T, bool>> False<T>() { return f => false; } 

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> exp_flow, Expression<Func<T, bool>> expression2) 
{ 

var invokedExpression = System.Linq.Expressions.Expression.Invoke(expression2, exp_flow.Parameters.Cast<System.Linq.Expressions.Expression>()); 

return System.Linq.Expressions.Expression.Lambda<Func<T, bool>>(System.Linq.Expressions.Expression.Or(exp_flow.Body, invokedExpression), exp_flow.Parameters); 

} 
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> exp_flow, Expression<Func<T, bool>> expression2) 
{ 

var invokedExpression = System.Linq.Expressions.Expression.Invoke(expression2, exp_flow.Parameters.Cast<System.Linq.Expressions.Expression>()); 

return System.Linq.Expressions.Expression.Lambda<Func<T, bool>>(System.Linq.Expressions.Expression.And(exp_flow.Body, invokedExpression), exp_flow.Parameters); 

} 

} 

After the first step is done, we can call this combination at the application level. Here, we will take the FeedBack table object as an example, and the code for the layer call is as follows:
I'm just going to list the core code. Note :PageNavigator1 is the paging control for my page.
Paging code:
 
private void ListDataBind(int pageIndex) 
{ 
int rowCount = 0; 
int pageCount = 0; 
int pageSize = 30; 
Expression<Func<FeedBack, bool>> expr = PredicateExtensionses.True<FeedBack>(); 
GetCondition(ref expr); 
var hs = from h in hm.AllFeedBacks.Where(expr) select h;// Lazy loading , The database has no operations  
if (pageIndex == 1)// If it is the first 1 Once the data , The total number of qualified record entries needs to be obtained  
{ 
rowCount = hs.Count();// Database implementation 1 time Count operation  
} 
else// The number of subsequent records , Gets from the properties of the paging control persistence state , save 1 time Count The query  
{ 
rowCount = PageNavigator1.RecordCount; 
} 
pageCount = rowCount > pageSize ? Convert.ToInt32((rowCount - 1) / pageSize) + 1 : 1;// General paging algorithm  
if (pageIndex > pageCount) 
{ 
pageIndex = pageCount; 
} 
var pageData = hs.Skip(pageSize * (pageIndex - 1)).Take(pageSize);// This is also lazy loading , The database does not operate at this time  
FeedBackManageList.DataSource = pageData;// This is where the data is officially loaded , Only requests are made to the database 30 records SQL 
FeedBackManageList.DataBind(); 
PageNavigator1.RecordCount = rowCount;//  Feed paging control 1 Some of the data  
PageNavigator1.PageCount = pageCount;// Feed paging control 1 Some of the data  
PageNavigator1.PageIndex = pageIndex;// Feed paging control 1 Some of the data  
} 

Next comes the key part, the composition conditions, and notice here that we are using the composition class defined in step 1:
 
private void GetCondition(ref Expression<Func<FeedBack, bool>> expr) { 
int isLock = Int32.Parse(ddlIsLock.SelectedValue); 
if (isLock > -1) 
{ 
expr = expr.And(c => (c.IsLock == isLock));//1 A combination  
} 
string keyword = tbxKeyword.Text.FilterInjectStr(); 
if (!keyword.IsNullOrEmpty()) 
{ 
expr = expr.And(c => (c.HotelName.IndexOf(keyword) > -1)); //2 A combination  
} 
} 

At this point, we have completed the linq to Sql multi-conditional composite query with minimal database requests.
In addition, it should be noted that any operation on the data source is best done by lazy loading, otherwise it is possible to load all the data.
For example, let's write code like this :List < FeedBack > fbs = hm.AllFeedBacks.Where(c= > c.Id > 1000). ToList (); The consumption will be very serious! This is where all the data from the FeedBack table is loaded! So, be careful how you write it.

Conclusion: Microsoft linq to sql bring us convenient, at the same time also planted a lot of hidden dangers, such as more convenience to a lazy person like me, but not to think, often one is not careful, it load data, caused the waste of resources. At the same time of enjoying the convenience, we should pay attention to timely, in order to make them serve us better.

Related articles: