title: How to change the generic type of a lambda function in C#
body: <p>I am facing the following problem with conversion of the inner type in <code>Predicate&lt;T&gt;</code>. I have a class implementing a basic interface. Additionally to the interface, the class has some advanced properties and methods.</p>
  <pre class="lang-cs prettyprint-override"><code>public interface IBasic { /* ... */ }
  public class Advanced : IBasic { /* ... */ }
  </code></pre>
  <p>In a method I get a number of predicates over the <code>Advanced</code> class and I would like to sort them into two lists: 1) predicates that use only the <code>IBasic</code> interface 2) predicates that need all features from the <code>Advanced</code> class.</p>
  <pre><code>List&lt;Predicate&lt;IBasic&gt;&gt; basic = new List&lt;Predicate&lt;IBasic&gt;&gt;();
  List&lt;Predicate&lt;Advanced&gt;&gt; advanced = new List&lt;Predicate&lt;Advanced&gt;&gt;();
  
  public void SetPredicates(params Predicate&lt;Advanced&gt;[] predicates)
  {
      foreach(var item in predicates)
      {
          //Of course the `is` keyword does not work here, always returning false.
          //Is it possible to do the check on the lambda function in item in a different way?
          if (item is Predicate&lt;IBasic&gt;)
              basic.Add((Predicate&lt;IBasic&gt;)item); //And this cast throws an exception of course.
          else
              advanced.Add(item);
      }
  }
  </code></pre>
  <h3>Questions</h3>
  <ul>
  <li>Is it possible at all?</li>
  <li>How can I check if the generic type of the predicate can be reduced to the <code>IBasic</code> interface?</li>
  <li>How can I perform the predicate cast?</li>
  </ul>
  <p>The number of predicates is low, so I am fine using slower things like reflection of <code>dynamic</code> types.</p>
  <h3>Background</h3>
  <p>The reason for the partition of predicates is that I have lots of <code>Advanced</code> instances. I want to pre-filter them first by predicates that require only the <code>IBasic</code> interface as those evaluate very fast. Then I will have to filter a much lower number of instances in the second pass with complex methods of <code>Advanced</code> (not in <code>IBasic</code>) since they take very long to evaluate.</p>