Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are looking for less lines you can replace the if/else with ternary operator:</p> <pre><code>query = SearchType == SearchType.And ? PredicateBuilder.True&lt;Job&gt;() : PredicateBuilder.False&lt;Job&gt;(); foreach (var predicate in predicates) { query = SearchType == SearchType.And ? query.And(predicate) : query.Or(predicate); } </code></pre> <p>for the <code>'and not' / 'or not'</code> part the <code>!</code> operator should do the trick.</p> <p><strong>PD:</strong> Did you test the <code>foreach</code> part is correctly setting the predicates?, as far as i remember you are building the expression that will be executed at later point in time, so you may have a literal reference just to the last set predicate in the final iteration, and that's why you must use a temp variable to save the value of each iteration.</p> <p><strong>EDIT:</strong> If you want to negate a expression programmatic, that's a tricky one, you can try something like:</p> <pre><code>internal static Expression&lt;Func&lt;Job, bool&gt;&gt; Description(string term, bool invert) { return NegateExp&lt;Func&lt;Job, bool&gt;&gt;(p =&gt; p.Description.Contains(term), invert); } </code></pre> <p>And the NegateExp method will be something like:</p> <pre><code>public static Expression&lt;TDelegate&gt; NegateExp&lt;TDelegate&gt;(Expression&lt;TDelegate&gt; expression, bool inverse) { if (inverse) { return Expression.Lambda&lt;TDelegate&gt;(Expression.Not(expression.Body), expression.Parameters); } return expression; } </code></pre> <p>You can take a look at this question for more examples <a href="https://stackoverflow.com/questions/2166591/is-there-any-way-to-negate-a-predicate">Is there any way to negate a Predicate?</a></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.
    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