title: C# Dynamic WHERE clause in a LINQ to SQL Query
body: <p>I would like to execute a LINQ query with a dynamic where clause depending on how many different options a user has entered for their criteria.</p>
  <p>Is this possible?</p>
  <p>I have posted code below of how I would like it to work.</p>
  <p>Anyone got any suggestions?</p>
  <p>P.S. I have attempted using the .Contains method (generating a WHERE IN on SQL, however the number of parameters was above the 2100 threshold and caused the error &quot;LINQ The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Too many parameters were provided in this RPC request. The maximum is 2100&quot;.</p>
  <pre><code>private struct ProductStruct
  {
      public long ProductID;
  }
  
  private struct FilterStruct
  {
      public long ProductTypeFieldID;
      public long ValueNumber;
  }
  
  List&lt;FilterStruct&gt; filterList = new List&lt;FilterStruct&gt;();
  filterList.Add(new FilterStruct { ProductTypeFieldID = 3, ValueNumber = 195 });
  filterList.Add(new FilterStruct { ProductTypeFieldID = 8, ValueNumber = 55 });
   
  List&lt;ProductStruct&gt; productList = new List&lt;ProductStruct&gt;();
   
  productList = (from pfv in dC.ProductFieldValues
                             where
                                  foreach (FilterStruct filter in filterList)
                                  {
                                      pfv.ProductTypeFieldID == filter.ProductTypeFieldID
                                      &amp;&amp; pfv.ValueNumber == filter.ValueNumber
                                  }
                             select new ProductStruct
                             {
                                 ProductID = pfv.ProductID
                             }).ToList();
  </code></pre>
  <p>EDIT</p>
  <p>This looks as if it could be handy, but doesnt work with a dynamic where in?</p>
  <pre><code>private void Option2()
  {
      try
      {
          LinqDataDataContext dataConnection = new LinqDataDataContext(ConnectionString);
  
          List&lt;FilterStruct&gt; filterList = new List&lt;FilterStruct&gt;();
              filterList.Add(new FilterStruct { ProductTypeFieldID = 3, ValueNumber = 195 });
              filterList.Add(new FilterStruct { ProductTypeFieldID = 8, ValueNumber = 55 });
  
          string whereClause = null;
          foreach (FilterStruct filter in filterList)
          {
              if (whereClause != null)
                  whereClause += &quot;AND &quot;;
  
              whereClause += string.Format(&quot;ProductID IN (SELECT ProductID FROM ProductFieldValue WHERE ProductTypeFieldID = {0} AND ValueNumber = {1})&quot;, filter.ProductTypeFieldID, filter.ValueNumber);
          }
  
          List&lt;ProductStruct&gt; productList = new List&lt;ProductStruct&gt;();
              productList = (from pfv in dataConnection.ProductFieldValues.Where(whereClause)
                              select new ProductStruct
                                    {
                                    ProductID = pfv.ProductID
                                    }).ToList();
      } 
      catch (Exception ex)
      {
          MessageBox.Show(ex.ToString());
      }
  }
  </code></pre>