title: Reflection to call generic method with lambda expression parameter
body: <p>I'm looking for a way to call a generic method with a lambda expression that calls Contains in an array of items. </p>
  
  <p>In this case I'm using Entity Framework Where method, but the scenario could be applied in other IEnumerables.</p>
  
  <p>I need to call the last line of the above code through Reflection, so I can use any type and any property to pass to the Contains method.</p>
  
  <pre><code>var context = new TestEntities();
  
  var items = new[] {100, 200, 400, 777}; //IN list (will be tested through Contains)
  var type = typeof(MyType); 
  
  context.Set(type).Where(e =&gt; items.Contains(e.Id)); //**What is equivalent to this line using Reflection?**
  </code></pre>
  
  <p>In research, I've noticed that I should use GetMethod, MakeGenericType and Expression to achieve that, but I couldn't figure out how to do it. It would be very helpful to have this sample so I can understand how Reflection works with Lambda and Generic concepts.</p>
  
  <p>Basically the objective is to write a correct version of a function like this:</p>
  
  <pre><code>//Return all items from a IEnumerable(target) that has at least one matching Property(propertyName) 
  //with its value contained in a IEnumerable(possibleValues)
  static IEnumerable GetFilteredList(IEnumerable target, string propertyName, IEnumerable searchValues)
  {
      return target.Where(t =&gt; searchValues.Contains(t.propertyName));
      //Known the following:
      //1) This function intentionally can't be compiled
      //2) Where function can't be called directly from an untyped IEnumerable
      //3) t is not actually recognized as a Type, so I can't access its property 
      //4) The property "propertyName" in t should be accessed via Linq.Expressions or Reflection
      //5) Contains function can't be called directly from an untyped IEnumerable
  }
  
  //Testing environment
  static void Main()
  {
      var listOfPerson = new List&lt;Person&gt; { new Person {Id = 3}, new Person {Id = 1}, new Person {Id = 5} };
      var searchIds = new int[] { 1, 2, 3, 4 };
  
      //Requirement: The function must not be generic like GetFilteredList&lt;Person&gt; or have the target parameter IEnumerable&lt;Person&gt;
      //because the I need to pass different IEnumerable types, not known in compile-time
      var searchResult = GetFilteredList(listOfPerson, "Id", searchIds);
  
      foreach (var person in searchResult)
          Console.Write(" Found {0}", ((Person) person).Id);
  
      //Should output Found 3 Found 1
  }
  </code></pre>
  
  <p>I'm not sure if the other questions address this scenario, because I don't think I could clearly understand how Expressions work.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>I can't use Generics because I only have the type and the property to be tested (in Contains) at run-time. In the first code sample, suppose "MyType" is not known at compile time. In the second code sample, the type could be passed as a parameter to the GetFilteredList function or could be get via Reflection (GetGenericArguments).</p>
  
  <p>Thanks,</p>