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

Source: Internet
Author: User

I didn't run my blog for half a year. I was not very diligent, I started my own business, and I didn't have time to come. But in this small project, I used LINQ to SQL. In this case, I must search for multiple combinations of conditions. Although I have only a few research on the Expression Tree, it seems that there is no other way to study it.

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:

  

Code

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:

 

Code

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!

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.