title: Expression.Lambda and query generation at runtime, simplest "Where" example
body: <p>I was trying to generate a simple Lambda Expression at runtime with no luck... something like this:</p>
  
  <pre><code>var result = queryableData.Where(item =&gt; item.Name == "Soap")
  </code></pre>
  
  <p>Here is my example class and a fixture queryable:</p>
  
  <pre><code>public class Item
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  
  IQueryable&lt;Item&gt; queryableData = ...;
  </code></pre>
  
  <p>Then I generate a lambda expression at runtime <strong>correct code follows</strong>:</p>
  
  <pre><code>//"item" in "item =&gt;..."
  var item = Expression
      .Parameter(typeof(Item), "item");
  
  //property of my item, this is "item.Name"
  var prop = Expression
      .Property(item, "Name");
  
  //then "Soap" in '... =&gt; item.Name=="Soap"'
  var value = Expression.Constant("Soap");
  
  //equality expression "==" in my primer
  var equals = Expression.Equal(prop, value);
  
  //then lambda
  var lambda = Expression.Lambda&lt;Func&lt;Item, bool&gt;&gt;(equals, item);
  
  //and here are the results    
  var results = queryableData.Where(lambda);
  </code></pre>
  
  <p>Big thanks to <strong>dtb</strong> for advice!</p>