tags:

views:

681

answers:

2

Hi, I have form with user defined filters ( combobox with column names, combobox with filter types and textbox with value).

How can I dynamicly add user defined filter into LINQ query?

Typical query looks like:

var qProducts = from p in db.Products
    where p.IsArchived == false
    order by p.ProductName select p;

I'm using LINQ (IQuerable Toolkit) for access data in SQL CE database.

+2  A: 

You might want to look at Dynamic LINQ from the VS2008 Samples. Then you could do something like:

var qProducts = db.Products
                  .Where( "IsArchived = {0}", archiveFilterValue )
                  .OrderBy( sortColumn + " " + sortDirection );
tvanfosson
+1, It looks good. I'll try it.
TcKs
+1  A: 

you can add each filter dynamically if it's needed, e.g.:

if (txtFilter1.Text!="") qProducts=qProducts.Where(s=>s.Name==txtFilter1.Text);
if (txtFilter2.Text!="") qProducts=qProducts.Where(s=>s.Field==txtFilter2.Text);
if (cboCombo1.SelectedValue!=0) qProducts=qProducts.Where(s=>s.price...

and so on

pomarc
I want to do it dynamicly with reuse on every filterable table.Writing "if" for every column in database is not practical.
TcKs