21

Is there a pattern using Linq to dynamically create a filter?

I have the need to create custom filtering on a list, in the past I would just dynamically create the SQL...it doesn't seem like this is possible with Linq.

Mark Biek
  • 146,731
  • 54
  • 156
  • 201
emcpadden
  • 1,461
  • 2
  • 14
  • 15

4 Answers4

19

Check out the Dynamic Linq Library from ScottGu's blog:

For example, below is a standard type-safe LINQ to SQL VB query that retrieves data from a Northwind database and displays it in a ASP.NET GridView control:

Dim Northwind As New NorthwindDataContext
Dim query = From q In Northwind.Products Where p.CategoryID = 2 And p.UnitPrice > 3 Order By p.SupplierID Select p

Gridview1.DataSource = query
GridView1.DataBind()

Using the LINQ DynamicQuery library I could re-write the above query expression instead like so

Dim Northwind As New NorthwindDataContext
Dim query = Northwind.Products .where("CategoryID=2 And UnitPrice>3") . OrderBy("SupplierId")
Gridview1.DataSource = query
GridView1.DataBind()

Notice how the conditional-where clause and sort-orderby clause now take string expressions instead of code expressions. Because they are late-bound strings I can dynamically construct them. For example: I could provide UI to an end-user business analyst using my application that enables them to construct queries on their own (including arbitrary conditional clauses).

Neysor
  • 3,893
  • 11
  • 34
  • 66
Geoff
  • 9,340
  • 7
  • 38
  • 48
  • Does this work? I get this : Error 28 Overload resolution failed because no accessible 'OrderBy' can be called with these arguments: Data type(s) of the type parameter(s) cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error. I can't see 'String' as a parameter option in the .OrderBy clause. – JohnnyBizzle May 31 '11 at 08:11
  • @JohnnyBizzle - It works. I use it in many projects. Double check that the dynamic.cs is in your project and that you have it referenced properly in your using declarations. – Geoff May 31 '11 at 10:43
  • That is great. I never knew you could do that. That will solve my problem with advanced search query building. Nice one +1 – Piotr Kula Jan 23 '13 at 21:13
11

Dynamic Linq is one way to go.

It may be overkill for your scenario. Consider:

IQueryable<Customer> query = db.Customers;

if (searchingByName)
{
  query = query.Where(c => c.Name.StartsWith(someletters));
}
if (searchingById)
{
  query = query.Where(c => c.Id == Id);
}
if (searchingByDonuts)
{
  query = query.Where(c => c.Donuts.Any(d => !d.IsEaten));
}
query = query.OrderBy(c => c.Name);
List<Customer> = query.Take(10).ToList();
Amy B
  • 108,202
  • 21
  • 135
  • 185
5

Dynamically Composing Expression Predicates

Shawn Miller
  • 7,082
  • 6
  • 46
  • 54
2

something like this?

var myList = new List<string> { "a","b","c" };
var items = from item in db.Items
            where myList.Contains(item.Name)
            select item;

that would create a sql statement like

SELECT * FROM Items [t0] where Name IN ('a','b','c')
Darren Kopp
  • 76,581
  • 9
  • 79
  • 93