In LINQ to SQL, how to solve the problem of multiple conditional queries, the answer, with the expression tree! (next) _ Practical skills

Source: Internet
Author: User
Tags rowcount static class
How to do this in a real sense delay loading, that is, only from the database at a time we need to use that part of the data. Through the study, there are the following methods:
First, we're going to create a new static class that holds various combinations of multiple conditional combination queries, such as Or,and, and so on. The code is as follows:
Copy Code code 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 invoke this combination from the concrete application level, where we still take the feedback table object as an example, the layer call code is as follows:
I'll just enumerate the core code, note: PageNavigator1 is my page's paging control.
Pagination Code:
Copy Code code as follows:

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;//deferred loading, database does not have any operations
if (PageIndex = = 1)//If it is the first time to fetch the data, you need to obtain the total number of records that meet the criteria
{
ROWCOUNT = HS. Count ();//database for one count operation
}
The number of record bars after else//, obtained from the properties of the paging control persistence state, omitting one count query
{
RowCount = Pagenavigator1.recordcount;
}
PageCount = rowcount > pageSize? Convert.ToInt32 ((rowCount-1)/pageSize) + 1:1;//Universal Paging algorithm
if (PageIndex > PageCount)
{
PageIndex = PageCount;
}
var pagedata = hs. Skip (PageSize * (pageIndex-1)). Take (pageSize)//This is also a deferred load, the database does not operate at this time
Feedbackmanagelist.datasource = pagedata;//here to officially load the data, just send a request to the database 30 records SQL
Feedbackmanagelist.databind ();
Pagenavigator1.recordcount = rowcount;//gives pagination control some data
Pagenavigator1.pagecount = pagecount;//gives pagination control some data
Pagenavigator1.pageindex = pageindex;//gives pagination control some data
}

So here's the key part, the combination of conditions, and notice here we've used the combination class defined in the first step:
Copy Code code as follows:

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));/a combination
}
string keyword = tbxKeyword.Text.FilterInjectStr ();
if (!keyword. IsNullOrEmpty ())
{
expr = expr. and (c => (c.hotelname.indexof (keyword) > 1)); Two times combination
}
}

To this end, we have completed a LINQ to SQL multiple conditional combination query and minimized requests for the database.
In addition, to specifically note that any operation of the data source, preferably with deferred loading, otherwise, it will be possible to load all the data,
For example, we write this code:list<feedback> FBS = HM. Allfeedbacks.where (C=>c.id > 1000). ToList (); This will be very serious! Because the feedback table will be loaded with all the data! So be careful with this writing.

Summary: Microsoft's LINQ to SQL brings us convenience while also buried a lot of hidden dangers, such as the lazy people like me more convenient, but do not think, often accidentally loaded the data, resulting in the waste of resources. While enjoying these conveniences, attention should be paid to timely research, So that they are better served by us.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.