title: Dynamic LINQ using linqkit
body: <p>Following this example from <a href="http://www.albahari.com/nutshell/predicatebuilder.aspx" rel="nofollow">http://www.albahari.com/nutshell/predicatebuilder.aspx</a></p>
  
  <pre><code>IQueryable&lt;Product&gt; SearchProducts (params string[] keywords)
  {
    var predicate = PredicateBuilder.False&lt;Product&gt;();
  
    foreach (string keyword in keywords)
    {
      string temp = keyword;
      predicate = predicate.Or (p =&gt; p.Description.Contains (temp));
    }
    return dataContext.Products.Where (predicate);
  }
  </code></pre>
  
  <p>I have a similiar requirement except that firstly I have to do an AND query and secondly , unlike the above example, I have a bunch of  field names and a value to search in each of them. The field names and the values both are unknown till the user passes them through. I ended up doing something like the code below which works but obviously is very tightly coupled. </p>
  
  <pre><code> IEnumerable&lt;someClass&gt; qry = context.someClass;
   var pb = PredicateBuilder.True&lt;someClass&gt;();
  
   foreach (customClass rule in Filters)
              {
  
  
                  switch ( rule.field)
                      {
                      case "FirstName":
                              pb = pb.And(p=&gt;p.FirstName.Contains(rule.data));
                              break;
                      case "LastName":
                              pb = pb.And(p=&gt;p.LastName.Contains(rule.data));
                              break;
                      case "City":
                             pb = pb.And(p=&gt;p.City.Contains(rule.data));
                             break;
  
                      }
  
                  qry.AsQueryable().Where(pb);
              }
              return qry;
  </code></pre>
  
  <p>As can be seen from above the Rule class has a field and data property. What would be ideal is if I could dynamically create the predicatebuilder , something like </p>
  
  <pre><code>filterClause = "p=&gt;p." + rule.field + ".Contains(" + rule.data + ")";
  
  
  pb = pb.And(filterClause);
  
  qry.AsQueryable().Where(pb);
  </code></pre>
  
  <p>Thanks in advance for any inputs</p>