User defined filter for linq
Asked Answered
A

2

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.

Amaya answered 28/4, 2009 at 15:30 Comment(1)
This might help: social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/…Lifer
E
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 );
Eec answered 28/4, 2009 at 15:49 Comment(0)
K
-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

Kenya answered 28/4, 2009 at 15:53 Comment(1)
I want to do it dynamicly with reuse on every filterable table. Writing "if" for every column in database is not practical.Amaya

© 2022 - 2024 — McMap. All rights reserved.