Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create a System.Linq.Expressions.Expression for Like?
    primarykey
    data
    text
    <p>I created a filterable BindingList <a href="http://www.nablasoft.com/alkampfer/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/" rel="nofollow noreferrer">from this source</a>. It works great:</p> <pre><code>list.Filter("Customer == 'Name'"); </code></pre> <p>does what it should. The internals work like a parser, that converts the expression <code>==</code> or <code>!=</code> into <code>System.Linq.Expressions.Expression</code>. In this case, <code>==</code> becomes <code>System.Linq.Expressions.Expression.Equal</code>.</p> <p>Unfortunately <code>System.Linq.Expressions.Expression</code> does not contain a like operator and I don't know how to solve this.</p> <p>The initial code looks like this:</p> <pre><code>private static Dictionary&lt;String, Func&lt;Expression, Expression, Expression&gt;&gt; binaryOpFactory = new Dictionary&lt;String, Func&lt;Expression, Expression, Expression&gt;&gt;(); static Init() { binaryOpFactory.Add("==", Expression.Equal); binaryOpFactory.Add("&gt;", Expression.GreaterThan); binaryOpFactory.Add("&lt;", Expression.LessThan); binaryOpFactory.Add("&gt;=", Expression.GreaterThanOrEqual); binaryOpFactory.Add("&lt;=", Expression.LessThanOrEqual); binaryOpFactory.Add("!=", Expression.NotEqual); binaryOpFactory.Add("&amp;&amp;", Expression.And); binaryOpFactory.Add("||", Expression.Or); } </code></pre> <p>Then I created an expression that will do what I want:</p> <pre><code>private static System.Linq.Expressions.Expression&lt;Func&lt;String, String, bool&gt;&gt; Like_Lambda = (item, search) =&gt; item.ToLower().Contains(search.ToLower()); private static Func&lt;String, String, bool&gt; Like = Like_Lambda.Compile(); </code></pre> <p>e.g.</p> <pre><code>Console.WriteLine(like("McDonalds", "donAld")); // true Console.WriteLine(like("McDonalds", "King")); // false </code></pre> <p>But <code>binaryOpFactory</code> requires this:</p> <pre><code>Func&lt;Expression, Expression, Expression&gt; </code></pre> <p>The predefined expressions seem to be exactly that:</p> <pre><code>System.Linq.Expressions.Expression.Or; </code></pre> <p>Can anyone tell me how to convert my expression?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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