title: Build LINQ queries dynamically - force using sp_executesql instead of raw query
body: <p>I need to build queries dynamically (entity classes have over 30 properties and I want to avoid multiple if statements).
  I solved this using reflection and expression tree (iterate by properies and append predicate if some search criteria is specified).
  The problem is that it generates raw SQL script.</p>
  
  <p>Example</p>
  
  <pre><code>SELECT 
      [Extent1].[ProductId] AS [ProductId], 
      [Extent1].[ManufacturerId] AS [ManufacturerId], 
      [Extent1].[Title] AS [Title]
      FROM [dbo].[Products] AS [Extent1]
      WHERE 3 = [Extent1].[ManufacturerId]
  </code></pre>
  
  <p>I need to obtain query using <strong>sp_executesql</strong> procedure (resuing the same query execution plan).</p>
  
  <pre><code>exec sp_executesql N'SELECT 
      [Extent1].[ProductId] AS [ProductId], 
      [Extent1].[ManufacturerId] AS [ManufacturerId], 
      [Extent1].[Title] AS [Title]
      FROM [dbo].[Products] AS [Extent1]
      WHERE [Extent1].[ManufacturerId] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=3
  </code></pre>
  
  <p>It turned out the problem is in the method below. It uses Expression.Constant to create Expression object. I don't know what should I replace it with.</p>
  
  <pre><code>public static Expression&lt;Func&lt;TItem, bool&gt;&gt; PropertyEqual&lt;TItem, TValue&gt;(
      this PropertyInfo property, TValue value)
  {
      var param = Expression.Parameter(typeof(TItem));
      var body = Expression.Equal(Expression.Property(param, property),
          Expression.Constant(value, typeof(TValue)));
      return Expression.Lambda&lt;Func&lt;TItem, bool&gt;&gt;(body, param);
  }
  </code></pre>
  
  <p>How can I resolve the problem?</p>