I have a user control which takes a Func which it then gives to the Linq "Where" extension method of a IQueryable. The idea is that from the calling code, I can pass in the desired search function.
I'd like to build this search function dynamically as such:
Func<Order, bool> func == a => true;
if (txtName.Text.Length > 0) {
//add it to the function
func = a => func(a) && a.Name.StartsWith(txtName.Text);
}
if (txtType.Text.Length > 0) {
//add it to the function
func = a => func(a) && a.Type == txtType.Text;
}
..... etc .....
The problem with this approach is that since I'm reusing the name "func" it creates a recursive function.
Is there an easy way to build out the expression tree like this to make a dynamic where clause (in the absence of having the IQueryable up front and repeatedly calling "Where")?