2

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.

1

2 Answers 2

5

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 );
0
-1

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

1
  • 3
    I want to do it dynamicly with reuse on every filterable table. Writing "if" for every column in database is not practical.
    – TcKs
    Commented Apr 28, 2009 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.