Custom filter for linq

I have a form with custom filters (combobox with column names, combobox with filter types and text box with value).

How can I dynamically add a user-defined filter to a LINQ query?

A typical query looks like this:

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

I am using LINQ (IQuerable Toolkit) to access data in a SQL CE database.

+1
filter linq
Apr 28 '09 at 3:30 p.m.
source share
2 answers

You can see Dynamic LINQ from VS2008 Samples . Then you can do something like:

 var qProducts = db.Products .Where( "IsArchived = {0}", archiveFilterValue ) .OrderBy( sortColumn + " " + sortDirection ); 
+3
Apr 28 '09 at 15:49
source share

you can add each filter dynamically if necessary, for example:

 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... 

etc.

-one
Apr 28 '09 at 15:53
source share



All Articles