title: C# Entity Framework Linq: Select Where If/Else
body: <p>Is it possible to do a <code>.Where</code> with a series of nested <code>if</code> statements?</p>
  
  <p>Here's what I'd like to do:</p>
  
  <pre><code>public class Repository {
      public async Task&lt;IQueryable&lt;Order&gt;&gt; orders (int? accountId, int? userId) {
          IQueryable&lt;Order&gt; orders;
  
          orders = _context.Orders
              .Where(o =&gt;
              {
                  int filterCount = 0;
                  int filterCriteriaMet = 0;
  
                  // if the accountId param is not null
                  // increment the filter counter
                  // and check if this order's accountId == param
                  // if so, increment filterCriteriaMet counter
                  if (accountId != null)
                  {
                      filterCount++;
  
                      if (o.AccountId == accountId)
                      {
                          filterCriteriaMet++;
                      }
                  }
  
                  // if the userId param is not null
                  // increment the filter counter
                  // and check if this order's userId == param
                  // if so, increment filterCriteriaMet counter
                  if (userId != null)
                  {
                      filterCount++;
  
                      if (o.UserId == userId) {
                          filterCriteriaMet++;
                      }
                  }
  
                  return filterCount == filterCriteriaMet;
              });
  
          return orders;
      }
  }
  </code></pre>
  
  <p>but this gives me the following error:</p>
  
  <blockquote>
    <p>A lambda expression with a statement body cannot be converted to an expression tree</p>
  </blockquote>