title: MongoDB Linq query in C# with filters
body: <p>I am trying to do a LINQ query on several Mongo collections. All the collections have to be joined based on ApplicationId and an outer Join has to be done - so that persons that have no statuses are returned as well.
  The JOIN part and everything around it works as expected. The problem is that when I add a filter to one of the collections, the whole thing breaks</p>
  
  <pre><code>An exception of type 'System.ArgumentException' occurred in System.Linq.Expressions.dll but was not handled in user code: 'Expression of type 'System.Collections.Generic.IEnumerable`1[CDM.Person]' cannot be used for parameter of type 'System.Linq.IQueryable`1[CDM.Person]' of method 'System.Linq.IQueryable`1[CDM.Person] Where[Person](System.Linq.IQueryable`1[CDM.Person], System.Linq.Expressions.Expression`1[System.Func`2[CDM.Person,System.Boolean]])''
  </code></pre>
  
  <p>Here is my query</p>
  
  <pre><code>var applications = _dbContext.GetCollection&lt;Application&gt;(typeof(Application).Name).AsQueryable().Where(
                  x =&gt; x.OrganizationID == TokenContext.OrganizationID);
  
  var persons = _dbContext.GetCollection&lt;Person&gt;(typeof(Person).Name).AsQueryable().Where(p =&gt;p.FirstName == "j");
  var statuses = _dbContext.GetCollection&lt;ApplicationStatus&gt;(typeof(ApplicationStatus).Name).AsQueryable();
  var mortgages = _dbContext.GetCollection&lt;Mortgage&gt;(typeof(Mortgage).Name).AsQueryable();
  
              var statusQuery = from a in applications
                                  join p in persons on a.ApplicationID equals p.ApplicationID 
                                  join s in statuses on a.ApplicationID equals s.ApplicationID into pas
                                  join m in mortgages on a.ApplicationID equals m.ApplicationID into morgs
                                  from subWHatever in pas.DefaultIfEmpty()
                                select new ApplicationStatusProjection
                                {
                                    ApplicationId = a.ApplicationID,
                                    FirstName = p.FirstName,
                                    LastName = p.Surname,
                                    Prefix = p.Prefix,
                                    DateOfBirth = p.DateOfBirth,
                                    Initials = p.Initials,
                                    PostalCode = p.Addresses.First().PostalCode,
                                    MortgageAmount = morgs.Sum(i =&gt; i.MortgageTotal) ?? 0,
                                    StatusExpireAt = subWHatever.ExpireAt ?? DateTime.MinValue,
                                    StatusMessageText = subWHatever.MessageText ?? "",
                                    StatusMessage = subWHatever.MessageStatus ?? ""
  
                                };
  
              if (!String.IsNullOrEmpty(orderBy))
              {
                  statusQuery = statusQuery?.OrderBy(orderBy);
              }
  
              if (nrOfRecords != null)
              {
                  statusQuery = statusQuery?.Take(nrOfRecords.Value);
              }
  
              // Execute the query
              var result = statusQuery?.ToList();
              return result;
  </code></pre>
  
  <p>I followed the guidelines here <a href="https://mongodb.github.io/mongo-csharp-driver/2.6/reference/driver/crud/linq/" rel="nofollow noreferrer">https://mongodb.github.io/mongo-csharp-driver/2.6/reference/driver/crud/linq/</a> and I also tried this</p>
  
  <pre><code>var statusQuery = 
      from a in applications
         join p in persons on a.ApplicationID equals p.ApplicationID into pa
      from paObject in pa.DefaultIfEmpty()
         join s in statuses on paObject.ApplicationID equals s.ApplicationID into pas
  </code></pre>
  
  <p>But I got the same error as before.</p>
  
  <p>Thank you in advance.</p>