title: How to add a '.Contains()' to the where clause of a query?
body: <p>I have this query but I need to add a <em>where</em> clause that uses 'Contains()' so I can check the db for a list of accountIDs, the reason I need to do it this way is because I have to first check if there are any accountID's and if there are it must execute the 'Contains()' in the where clause but the compiler complains if I try do it this way but given what the method has to do I can't figure out another way to do it given the fact that there are so many tables joined in the query.</p>
  
  <p>My code:</p>
  
  <pre><code>bool UseAcc = false;
  
  if (accountIds != null &amp;&amp; accountIds.Count &gt; 0)
  {
      UseAcc = true;
  }
  
  var query = (from i in db.Incidents
               join s in db.Sites on i.SiteID equals s.SiteID
               join a in db.Accounts on i.AccountID equals a.AccountID
               join st in db.Status on i.StatusID equals st.StatusID
               join currentUser in db.Users on i.CurrentAssignedUser equals currentUser.UserID
               join loggedByUser in db.Users on i.LoggedByUser equals loggedByUser.UserID
               join createdByUser in db.Users on i.CreatedByUser equals createdByUser.UserID
               join l in db.Locations on i.Location equals l.LocationID into locList
               from loc in locList.DefaultIfEmpty()
               join q in db.QuestionCategories on i.QuestionCategoryID equals q.QuestionCategoryID
               join ia in db.IncidentActions on i.IncidentID equals ia.IncidentID into iaList
               from actions in iaList.DefaultIfEmpty()
  
              //If 'UseAcc' gets set to true then execute the contains section of the or statement
               where (i.Active == true) &amp;&amp;
                     (UseAcc = false || a =&gt; accountIds.Contains(a.AccountID))
  
               select new
               {
                   Title = i.Title,
                   IncidentID = i.IncidentID,
                   StatusName = st.StatusName,
                   StatusID = i.StatusID,
                   ReferenceNumber = i.ReferenceNo,
                   AccountName = a.AccountName,
                   AccountID = i.AccountID,
                   SiteName = s.SiteName,
                   SiteID = i.SiteID,
                   LocationName = loc.LocationName,
                   LocationID = i.Location,
                   CatName = q.QuestionCategoryName,
                   CatID = i.QuestionCategoryID,
                   CurrentAssignedUser = currentUser.FirstName + " " + currentUser.LastName,
                   AssignedUserID = i.CurrentAssignedUser,
                   CreatedByUser = createdByUser.FirstName + " " + createdByUser.LastName,
                   DateCreated = i.LoggedDate,
                   DepartmentID = i.DepartmentID,
                   Logger = loggedByUser.FirstName + " " + loggedByUser.LastName,
                   LoggedBy = i.LoggedByUser,
                   EscalationCount = i.EscalationCount)
               });
  </code></pre>
  
  <p>I would paste the whole method but its really big.</p>
  
  <p>I have looked at examples online but they can't work either because alot of them use an AsQueryable with a single table and this query has many tables joined. </p>
  
  <p>Please Help.</p>