<p>Is there a pattern using Linq to dynamically create a filter?</p>

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

[{'answer_id': 30887, 'author': 'Geoff', 'author_id': 1097, 'author_profile': 'https://Stackoverflow.com/users/1097', 'pm_score': 5, 'selected': True, 'text': '<p>Check out the <a href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" rel="noreferrer">Dynamic Linq Library</a> from ScottGu\'s blog:</p>\n\n<p>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:</p>\n\n<pre><code>Dim Northwind As New NorthwindDataContext\nDim query = From q In Northwind.Products Where p.CategoryID = 2 And p.UnitPrice &gt; 3 Order By p.SupplierID Select p\n\nGridview1.DataSource = query\nGridView1.DataBind()\n</code></pre>\n\n<p>Using the LINQ DynamicQuery library I could re-write the above query expression instead like so</p>\n\n<pre><code>Dim Northwind As New NorthwindDataContext\nDim query = Northwind.Products .where("CategoryID=2 And UnitPrice&gt;3") . OrderBy("SupplierId")\nGridview1.DataSource = query\nGridView1.DataBind()\n</code></pre>\n\n<p>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).</p>\n'}, {'answer_id': 30895, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 2, 'selected': False, 'text': '<p>something like this?</p>\n\n<pre><code>var myList = new List&lt;string&gt; { "a","b","c" };\nvar items = from item in db.Items\n            where myList.Contains(item.Name)\n            select item;\n</code></pre>\n\n<p>that would create a sql statement like</p>\n\n<pre><code>SELECT * FROM Items [t0] where Name IN (\'a\',\'b\',\'c\')\n</code></pre>\n'}, {'answer_id': 30934, 'author': 'Shawn Miller', 'author_id': 247, 'author_profile': 'https://Stackoverflow.com/users/247', 'pm_score': 3, 'selected': False, 'text': '<p><a href="http://www.albahari.com/nutshell/predicatebuilder.html" rel="noreferrer">Dynamically Composing Expression Predicates</a></p>\n'}, {'answer_id': 241290, 'author': 'Amy B', 'author_id': 8155, 'author_profile': 'https://Stackoverflow.com/users/8155', 'pm_score': 4, 'selected': False, 'text': '<p>Dynamic Linq is one way to go.</p>\n\n<p>It may be overkill for your scenario.  Consider:</p>\n\n<pre><code>IQueryable&lt;Customer&gt; query = db.Customers;\n\nif (searchingByName)\n{\n  query = query.Where(c =&gt; c.Name.StartsWith(someletters));\n}\nif (searchingById)\n{\n  query = query.Where(c =&gt; c.Id == Id);\n}\nif (searchingByDonuts)\n{\n  query = query.Where(c =&gt; c.Donuts.Any(d =&gt; !d.IsEaten));\n}\nquery = query.OrderBy(c =&gt; c.Name);\nList&lt;Customer&gt; = query.Take(10).ToList();\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/30879', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2858/']