title: Multiple search parameters with LINQ
body: <p>I'm writing what I believe should be a relatively straight-forward Windows Form app. I'm using LINQ to SQL, though I have never used it before. We have a SQL Server database, and I'm creating a front-end to access that database. I'm trying to figure out the most efficient way to search for multiple (arbitrary number of) search parameters with it.</p>
  
  <p>In the windows form, I create a dictionary with each search key and its value to search for, and pass it into my search() method. I am trying to find a way to search the database with each of those keys and their associated values. Here is what I am trying to do:</p>
  
  <pre><code>public IQueryable&lt;Product&gt; Search(Dictionary&lt;string, string&gt; searchParams)
  {
     DBDataContext dc = new DBDataContext();
     var query = dc.Products;
  
     foreach (KeyValuePair&lt;string, string&gt; temp in searchParams)
     {
        query = query.Where(x =&gt; x.(temp.Key) == temp.Value);
     }
  
     return query;
  }
  </code></pre>
  
  <p>I realize that syntactically x.(temp.Key) is incorrect, but I hope that illustrates what I am trying to do. I was wondering if there is another way to go about what I am trying to do without having to do a giant switch statement (or if/else if tree).</p>
  
  <p>EDIT</p>
  
  <p>So, I revised it a little, but I'm still having issues with it. Here is what I currently have:</p>
  
  <pre><code>public IQueryable&lt;Product&gt; Search(Dictionary&lt;string, string&gt; searchParams)
  {
      DBDataContext dc = new DBDataContext();
  
      string sQuery = "";
      foreach (KeyValuePair&lt;string, string&gt; temp in searchParams)
      {
          sQuery += temp.Key + "=" + temp.Value + " AND ";
      }
  
      var query = dc.Products.Where(sQuery);
  
      return query;
  }
  </code></pre>
  
  <p>According to the LINQ Dynamic Query Library article, this should be OK. Here is the error I'm getting:</p>
  
  <p>The type arguments for method 'System.Linq.Queryable.Where(System.Linq.IQueryable, System.Linq.Expressions.Expression>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.</p>