title: C# Dynamic Linq Where Clause
body: <p>is there a way to dynamically build a Where clause in LINQ?  </p>
  
  <p>Example... today I query my sql db, and only ServerGroup A has issues.  I return the fault codes for that group.  Works. However, tomorrow, ServerGroup A &amp; B have issues. I do not want to manually change my LINQ query, but have it dynamically add the new ServerGroup to the WHERE clause.  Is this possible with LINQ?  I've done it with a Sql query.</p>
  
  <p>Greatly appreciate the assistance. </p>
  
  <pre><code>        var query = referenceDt.AsEnumerable()
              .Where(results =&gt; results.Field&lt;string&gt;("ServerGroup") == "A" ||
                               results.Field&lt;string&gt;("SeverGroup") == "B")
              .GroupBy(results =&gt; new
                  {
                      FaultCode = results.Field&lt;int&gt;("FaultCode")
                  })
              .OrderByDescending(newFaultCodes =&gt; newFaultCodes.Key.FaultCode)
              .Select(newFaultCodes =&gt; new
              {
                  FaultCode = newFaultCodes.Key.FaultCode,
                  Count = newFaultCodes.Count()
              });
  </code></pre>