How to solve the problem of multi-conditional query in LINQ to SQL, answer, use expression tree!_ practical skills

Source: Internet
Author: User
This article is suitable for the small amount of data, for large data, need to use another way, see the next
First of all, from the Internet search, is essential, probably understand the LINQ multiple conditional combination query direction, began to start, first, write a delegate method:
Copy Code code as follows:

private bool Getcondition (FeedBack FB)
{
bool Boolresult = true;
int f_type = Int32.Parse (Ddlftype.selectedvalue);
int isclose = Int32.Parse (Ddlisclose.selectedvalue);
int isTrue = Int32.Parse (Ddlistrue.selectedvalue);
string keyword = tbxKeyword.Text.FilterInjectStr ();
if (f_type!= 0)
{
Boolresult &= FB. F_type = = F_type;
}
if (Isclose!=-1)
{
Boolresult &= FB. Isclose = = Isclose;
}
if (isTrue!=-1)
{
Boolresult &= FB. IsTrue = = IsTrue;
}
if (!keyword. IsNullOrEmpty ())
{
Boolresult &= FB. Contentinfo.indexof (keyword) >-1;
}
return boolresult;
}

Because it was a test, I put all the methods on the current page.
Note that the content of this method can be changed according to the actual situation, what I do here is a list of the message board;
Calling the way that I'm here the list is bound to the Repeater control.
So you can do this:
Copy Code code as follows:

private void Listdatabind ()
{
expression<func<feedback,bool>> expr = n => getcondition (n);
list<feedback> pagedata = FeedBacks.AllFeedBacks.Where (Expr.compile ()). ToList ();
Feedbacklist.datasource = Pagedata;
Feedbacklist.databind ();
}

Note that feedback is the object name of the data source table for my LINQ to SQL.
In addition, the site search some of the answers are wrong, such as Will my expression<func<feedback,bool>> written expressionbool>>,nnd, despise these spam sites. and a writer.
I hope you beginners can benefit from my article!

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.