How to solve the multi-condition query problem and answer in linq to SQL, using Expression Tree!

Source: Internet
Author: User

This article is suitable for small data volumes. For large data volumes, another method is required. For more information, see the next article.
First of all, searching on the internet is essential. After learning about the direction of the Multi-condition combination query of linq, you can start to do it. First, write a delegate method:
Copy codeThe Code is 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;
}

Here, I put all the methods on the current page for testing.
Note that the content in this method can be changed according to the actual situation. Here I am making a message board list;
Call method. Here, the list is bound to the repeater control.
So we can do this:
Copy codeThe Code is 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: FeedBack is the object name of the data source table of my linq to SQL.
In addition, some answers to the website search are incorrect. For example, my Expression <Func <FeedBack, bool> is written as Expressionbool>, NND, despising these junk websites, and writers.
I hope you will benefit from my article!

Related 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.