title: C# Linq: Combine multiple .Where() with an *OR* clause
body: <p>I have been searching a lot about my current problem but I could not find a real answer to solve that issue. </p>
  
  <p>I am trying to build a LINQ Query that produces the following SQL:</p>
  
  <pre><code>SELECT * FROM TABLE WHERE (Field1 = X, Field2 = Y ... ) or (Field3 = Z)
  </code></pre>
  
  <p>In a normal situation I would just do this:</p>
  
  <pre><code>Object.Where(c =&gt; (c.Field1 == X &amp;&amp; c.Field2 == Y) || (c.Field3 == Z))
  </code></pre>
  
  <p>I cannot use this approach because the query is build by using multiple <code>.Where()</code> calls.</p>
  
  <p>Having an example:</p>
  
  <pre><code>// This is a short example, the real world situation has 20 fields to check and they are all connected with an AND.
  if (model.Field1.HasValue) 
  {
      Query = Query.Where(c =&gt; c.Field1 == X)
  }
  
  if (model.Field2.HasValue) 
  {
      Query = Query.Where(c =&gt; c.Field2 == X)
  }
  
  [...] like 20 more of these .Where() calls.
  </code></pre>
  
  <p>and that is how it gets complicated for me. All these <code>.Where()</code> calls are building a Linq Query that is connected with <code>AND</code>, which is fine.</p>
  
  <p>How do I let them execute with Parenthese and add a simple <code>OR</code> now using the API?</p>
  
  <p>Is there a way to save the predicate in some variables so I can make something like:</p>
  
  <pre><code>Query = Query.Where(c =&gt; previousPredicates || c.Field3 == X)
  </code></pre>
  
  <p>or how to solve that problem?</p>
  
  <p>I think there must be a good solution for that particual problem and I am not the only one who needs it, but I am absolute unsure how to achieve it.</p>
  
  <p>P.S: I can't really remove the multiple <code>.Where()</code> calls and writing direct SQL is neither an option.</p>
  
  <p><em>EDIT</em>
  StackOverflow wants me to say why my question is different from others. Well, the thing is about <code>Parentheses</code>. I do not want to connect all <code>.Where()</code> with a single OR clause, I want to leave them with <code>AND</code> and add another <code>OR</code> clause while all the <code>AND</code> queries are being parenthesied.</p>