title: Append OR subquery in Linq
body: <p>I am trying to build a simple search against some entities (EF4, if that makes any difference). Passed into my search query is a list of criteria objects. The crieteria object looks like this:</p>
  
  <pre><code>public class ClaimSearchCirtieria
  {
      public Guid? FinancialYear { get; set; }
      public bool AllClaimants { get; set; }
      public IList&lt;Guid&gt; ClaimantIds { get; set; }
      public bool AllExpenseCategories { get; set; }
      public IList&lt;ExpenseCategoryAndTypeCriteria&gt; EpenseCategoryAndTypes { get; set; }
  }
  </code></pre>
  
  <p>And the ExpenseCategoryAndTypeCriteria</p>
  
  <pre><code>public class ExpenseCategoryAndTypeCriteria
  {
      public Guid ExpenseCategory { get; set; }
      public bool AllTypesInCatgeory { get; set; }
      public IList&lt;Guid&gt; ExpenseTypes { get; set; }
  }
  </code></pre>
  
  <p>Searching on financial years and claimants needs to be an AND query, then I need the expense categories and expense types to be appended as an OR sub query.</p>
  
  <p>In essence I'm trying to do:</p>
  
  <pre><code>select * 
  from claims
  where &lt;financial year&gt; AND &lt;Claimants&gt; AND (expense type 1 OR expense type 2 or expense category X)
  </code></pre>
  
  <p>So far I've got this:</p>
  
  <pre><code>public PagedSearchResult&lt;Claim&gt; Search(ClaimSearchCirtieria searchCriteria, int page, int pageSize)
  {
      var query = All();
  
      if (searchCriteria.FinancialYear.HasValue)
      {
          query = from claim in query
                  where claim.FinancialYearId == searchCriteria.FinancialYear
                  select claim;
      }
  
      if (!searchCriteria.AllClaimants)
      {
          query = from claim in query
                  where searchCriteria.ClaimantIds.Contains(claim.ClaimantId)
                  select claim;
      }
  
      if (!searchCriteria.AllExpenseCategories)
      {
          foreach (var item in searchCriteria.EpenseCategoryAndTypes)
          {
              if (item.AllTypesInCatgeory)
              {
                  //Just search on the category
                  query = query.Where(claim =&gt;
                      (from transaction in claim.ClaimTransactions
                       where item.ExpenseCategory == transaction.ExpenseType.ExpenseCategoryId
                       select transaction).Count() &gt; 0
                  );
              }
              else
              {
                  //Search for the specified types
                  query = query.Where(claim =&gt;
                      (from transaction in claim.ClaimTransactions
                       where item.ExpenseTypes.Contains(transaction.ExpenseTypeId)
                       select transaction).Count() &gt; 0
                  );
              }
          }
      }
  
      return PagedSearchResult&lt;Claim&gt;.Build(query, pageSize, page);
  }
  </code></pre>
  
  <p>What I'm currently seeing is that the last expense category requested is the only expense category I get results for. Also, looking at the code, it looks like I would expect this to be building a series of AND queries, rather that the required OR.</p>
  
  <p>Any pointers?</p>