Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is actually possible, but maybe in a slightly different form than you're used to. In .NET, lambda expressions can either be interpreted as delegates <em>OR</em> as <a href="http://msdn.microsoft.com/en-us/library/bb397951.aspx" rel="noreferrer">expression trees</a>. It is relatively straightforward to perform a <code>NOT</code> operation on an expression tree.</p> <p>Here is a sample using your code as a starting point:</p> <pre><code>namespace Sample { using System; using System.Collections.Generic; using System.Linq.Expressions; internal class ExpressionSample { private static Expression&lt;TDelegate&gt; Negate&lt;TDelegate&gt;(Expression&lt;TDelegate&gt; expression) { return Expression.Lambda&lt;TDelegate&gt;(Expression.Not(expression.Body), expression.Parameters); } private static void Main() { // Match any string of length 2 or more characters Expression&lt;Predicate&lt;string&gt;&gt; expression = (s) =&gt; s.Length &gt; 1; // Logical negation, i.e. match string of length 1 or fewer characters Expression&lt;Predicate&lt;string&gt;&gt; negatedExpression = ExpressionSample.Negate(expression); // Compile expressions to predicates Predicate&lt;string&gt; predicate = expression.Compile(); Predicate&lt;string&gt; negativePredicate = negatedExpression.Compile(); List&lt;string&gt; list1 = new List&lt;string&gt; { string.Empty, "an item", "x", "another item" }; List&lt;string&gt; list2 = new List&lt;string&gt; { "yet another item", "still another item", "y", string.Empty }; list2.RemoveAll(negativePredicate); list2.AddRange(list1.FindAll(predicate)); list2.ForEach((s) =&gt; Console.WriteLine(s)); } } } </code></pre>
    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. 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.
    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