title: Expression vs Predicate issues
body: <p>I've got a bit of a challenge where I have to create an expression tree to represent a query input by the user. Since I don't have the time to create all the possible cases of user input, I figured that expression trees would aid me in solving this.</p>
  
  <p>For the most part, it has. I am, however, a bit stumped. I am in the code below trying to perform a List.Find with a dynamically created expression. The expression, in short, is this:</p>
  
  <pre><code>list.Find(m =&gt; m.ListOfStrings.Exists(s =&gt; s == "cookie"));
  </code></pre>
  
  <p>where m is</p>
  
  <pre><code>class MyClass
  {
      public List&lt;string&gt; ListOfStrings { get; set; }
  }
  </code></pre>
  
  <p>I've gotten so far as to create</p>
  
  <pre><code>s =&gt; s == "cookie"
  </code></pre>
  
  <p>with expressions, no problem. I've also declared a methodinfo for Exists</p>
  
  <pre><code>var existsMethod = typeof(MyClass)
          .GetProperty("ListOfStrings")
          .PropertyType
          .GetMethod("Exists");
  </code></pre>
  
  <p>The only problem I have is creating an expression to invoke said method with the lambda as a parameter like so</p>
  
  <pre><code>var findLambda = Expression.Lambda(
      Expression.Call(
          Expression.Property(
              Expression.Parameter(typeof(MyClass), "m"),
              typeof(MyClass).GetProperty("ListOfStrings")),
          existsMethod,
          existsLambda),
      Expression.Parameter(
          typeof (MyClass),
          "m"));
  </code></pre>
  
  <p>It gives an understandable exception that</p>
  
  <pre><code>Expression of type 'System.Func`2[System.String,System.Boolean]' cannot be used for parameter of type 'System.Predicate`1[System.String]' of method 'Boolean Exists(System.Predicate`1[System.String])'
  </code></pre>
  
  <p>How the heck can I overcome this?</p>
  
  <p>Full code:</p>
  
  <pre><code>private class MyClass
  {
      public List&lt;string&gt; ListOfStrings { get; set; }
  }
  
  public void SomeMethod()
  {
      var myObject = new MyClass();
      myObject.ListOfStrings = new List&lt;string&gt;();
      myObject.ListOfStrings.Add("cookie");
      myObject.ListOfStrings.Add("biscuit");
  
      List&lt;MyClass&gt; list = new List&lt;MyClass&gt;();
      list.Add(myObject);
  
      var existsLambda = Expression.Lambda(
          Expression.Equal(
              Expression.Parameter(typeof(string), "s"),
              Expression.Constant("cookie")),
          Expression.Parameter(typeof(string), "s"));
  
      var existsMethod = typeof(MyClass).GetProperty("ListOfStrings").PropertyType.GetMethod("Exists");
  
      var findLambda = Expression.Lambda(
          Expression.Call(
              Expression.Property(
                  Expression.Parameter(typeof(MyClass), "m"),
                  typeof(MyClass).GetProperty("ListOfStrings")),
              existsMethod,
              existsLambda),
          Expression.Parameter(
              typeof (MyClass),
              "m"));
  
      list.Find((Predicate&lt;MyClass&gt;)findLambda.Compile());
  }
  </code></pre>