tags:

views:

35

answers:

1

I have for example table cars

table cars(
producer varchar(30),
model varchar(30),
price integer,
start datetime,
end datetime)

and I need to return data from table using criterions ( based on one criterion , or two or all of them ). Is there any way to dynamically create linq question (just one linq query, not to create for all combinations different linq queries)? I can use some default values and search always buy all but that decrease my performance when I need to search by one or none.

A: 

The short answer is yes.

You can start with Dynamic LINQ Where Extension to get something up and running but it will need a few enhancements to deal with DateTimes.

I use the above in conjunction with a loop to deal with the multiple criteria:

var q = from c in db.Cars select c;

filters.ForEach (filter =>
{
   q = q.Where (filter.Field, filter.Value, filter.Comparison);
});
Todd Smith
Thanks Bender, I will try this !
Rebecca