Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Read this great post on DLINQ by ScottGu</p> <p><a href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" rel="nofollow noreferrer">Dynamic LINQ (Part 1: Using the LINQ Dynamic Query Library)</a> </p> <p>You would need something like</p> <pre><code>var product = myobject.Where("Cost &lt;= 10000"); var product = myobject.Where("Name = @0", strName); </code></pre> <p>If you downloaded the samples you need to find the <code>Dynamic.cs</code> file in the sample. You need to copy this file into your project and then add <code>using System.Linq.Dynamic;</code> to the class you are trying to use Dynamic Linq in.</p> <p>EDIT: To answer your edit. Yes, there is of course a performance difference. If you know the variations of filters beforehand then I would suggest writing them out without using DLINQ. </p> <p>You can create your own Extension Method like so.</p> <pre><code> public static class FilterExtensions { public static IEnumerable&lt;T&gt; AddFilter&lt;T,T1&gt;(this IEnumerable&lt;T&gt; list, Func&lt;T,T1, bool&gt; filter, T1 argument ) { return list.Where(foo =&gt; filter(foo, argument) ); } } </code></pre> <p>Then create your filter methods.</p> <pre><code> public bool FilterById(Foo obj, int id) { return obj.id == id; } public bool FilterByName(Foo obj, string name) { return obj.name == name; } </code></pre> <p>Now you can use this on an <code>IEnumerable&lt;Foo&gt;</code> very easily.</p> <pre><code> List&lt;Foo&gt; foos = new List&lt;Foo&gt;(); foos.Add(new Foo() { id = 1, name = "test" }); foos.Add(new Foo() { id = 1, name = "test1" }); foos.Add(new Foo() { id = 2, name = "test2" }); //Example 1 //get all Foos's by Id == 1 var list1 = foos.AddFilter(FilterById, 1); //Example 2 //get all Foo's by name == "test1" var list2 = foos.AddFilter(FilterByName, "test1"); //Example 3 //get all Foo's by Id and Name var list1 = foos.AddFilter(FilterById, 1).AddFilter(FilterByName, "test1"); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload