title: dynamic linq contains
body: <p>i search for a dynamic linq-to-sql Contains (StartsWith / EndsWith) method.</p>
  
  <p>I’ve tried the following code, but it didn't work.</p>
  
  <p>Any ideas?</p>
  
  <pre><code>public static IQueryable&lt;T&gt; WhereContains&lt;T, V&gt;(this IQueryable&lt;T&gt; queryable, string propertyName, V propertyValue)
  {
      ParameterExpression pe = Expression.Parameter(typeof(T), "p");
      Expression left = Expression.Property(pe, propertyName);
      Expression right = Expression.Constant(propertyValue, typeof(V));
  
      IQueryable&lt;T&gt; x = queryable.Where&lt;T&gt;(
          Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(
              Expression.Call(
                  typeof(T).GetMethod("Contains"),
                  left,
                  right),
              new ParameterExpression[] { pe }));
  
      return x;
  }
  </code></pre>