Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>The easy way</strong></p> <p>Just use</p> <pre><code>if (comparison == ComparisonType.StartsWith) query = query.Where(e =&gt; e.StringProperty.StartsWith("SearchString")); else if ... </code></pre> <p><strong>The hard way</strong></p> <p>If you want to do something like this, either make sure your LINQ provider can be told of this new method somehow, and how it translates to SQL (unlikely), or prevent your method from ever reaching the LINQ provider, and provide the provider something it understands (hard). For example, instead of</p> <pre><code>query.Where(e =&gt; CompMethod(e.StringProperty, "SearchString", comparsionType)) </code></pre> <p>you can create something like</p> <pre><code>var query = source.WhereLike(e =&gt; e.StringProperty, "SearchString", comparsionType) </code></pre> <p>with the following code</p> <pre><code>public enum ComparisonType { StartsWith, EndsWith, Contains } public static class QueryableExtensions { public static IQueryable&lt;T&gt; WhereLike&lt;T&gt;( this IQueryable&lt;T&gt; source, Expression&lt;Func&lt;T, string&gt;&gt; field, string value, SelectedComparisonType comparisonType) { ParameterExpression p = field.Parameters[0]; return source.Where( Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;( Expression.Call( field.Body, comparisonType.ToString(), null, Expression.Constant(value)), p)); } } </code></pre> <p>You can even add additional criteria this way</p> <pre><code>var query = from e in source.WhereLike( e =&gt; e.StringProperty, "SearchString", comparsionType) where e.OtherProperty == 123 orderby e.StringProperty select e; </code></pre> <p><strong>The very, very hard way</strong></p> <p>It would (technically) be possible to rewrite the expression tree before the provider sees it, so you <em>can</em> use the query you had in mind in the first place, but you'd have to</p> <ul> <li>create a <code>Where(this IQueryable&lt;EntityType&gt; source, Expression&lt;Func&lt;EntityType, bool&gt;&gt; predicate)</code> to intercept the <code>Queryable.Where</code>,</li> <li>rewrite the expression tree, replacing your <code>CompMethod</code>, wherever it is, with one of the <code>String</code> methods,</li> <li>call the original <code>Queryable.Where</code> with the rewritten expression,</li> <li>and first of all, be able to follow the extension method above in the first place!</li> </ul> <p>But that's probably way too complicated for what you had in mind.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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