title: Generating a LINQ query using Expression Trees
body: <p><strong>Update</strong></p>
  
  <p>Thanks to Marc's help the AlphaPagedList class <a href="http://alphapagedlist.codeplex.com/" rel="nofollow noreferrer" title="is now available on CodePlex">is now available on CodePlex</a> if anyone is interested</p>
  
  <p><strong>Original</strong></p>
  
  <p>I'm trying to create an expression tree to return elements that start with a given charecter.</p>
  
  <pre><code>IList&lt;char&gt; chars = new List&lt;char&gt;{'a','b'};
  IQueryable&lt;Dept&gt;Depts.Where(x=&gt; chars.Contains(x.DeptName[0]));
  </code></pre>
  
  <p>I want this to be used on any IEnumerable where I provide a lamdba to the property to select on eg:</p>
  
  <p><code>Depts.Alpha(x=&gt;x.DeptName, chars);</code></p>
  
  <p>I've been trying this but having no luck at all, any help?</p>
  
  <pre><code>public static IQueryable&lt;T&gt; testing&lt;T&gt;(this IQueryable&lt;T&gt; queryableData, Expression&lt;Func&lt;T,string&gt;&gt; pi, IEnumerable&lt;char&gt; chars)
  {
  // Compose the expression tree that represents the parameter to the predicate.
  
  ParameterExpression pe = Expression.Parameter(queryableData.ElementType, "x");
  ConstantExpression ch = Expression.Constant(chars,typeof(IEnumerable&lt;char&gt;));
  // ***** Where(x=&gt;chars.Contains(x.pi[0])) *****
  // pi is a string property
  //Get the string property
  
  Expression first = Expression.Constant(0);
  //Get the first character of the string
  Expression firstchar = Expression.ArrayIndex(pi.Body, first);
  //Call "Contains" on chars with argument being right
  Expression e = Expression.Call(ch, typeof(IEnumerable&lt;char&gt;).GetMethod("Contains", new Type[] { typeof(char) }),firstchar);
  
  
  MethodCallExpression whereCallExpression = Expression.Call(
      typeof(Queryable),
      "Where",
      new Type[] { queryableData.ElementType },
      queryableData.Expression,
      Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(e, new ParameterExpression[] { pe }));
  // ***** End Where *****
  
  return (queryableData.Provider.CreateQuery&lt;T&gt;(whereCallExpression));
  }
  </code></pre>