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!