Search code examples
c#sqlentity-frameworklinqentity-framework-plus

Using the AND (&&) operator on IncludeFilter in Entity Framework Plus not bringing back nested / child object


I am just finding my feet with Entity framework Plus to bring back data that has a complex set of constraints / requirements. I am able to use it successfully, however I cannot get a nested / child object to be brought back when using the && operator inside of an IncludeFilter() method.

I have a Company object > each has multiple CommunicationLink objects > each CommunicationLink object has a single Communication object. I have worked out that adding multiple 'Include Filters' can be used to replicate 'Or' functionality (||), but I can't for the life of me use the AND operator to filter the results and get the nested object to come through. Below is example code:

Example 1:

//2 WHERE CLAUSES
var companiesData = _dbContext.Companies
     .IncludeFilter(comp => comp.CommunicationLinks
                .Where(cmli =>
                    string.Equals(cmli.Communication.Action, "PhoneOut")
                )
                .Where(cmli => cmli.UserId == comp.AccountManager) 
                .Select(comm => comm) //I believe this is where the problem lies
      )
      .Where(co =>
           string.Equals(co.Type, "Customer")
           && string.Equals(co.Status, "Active")
           && co.Deleted != 1 
       )
       .OrderBy(c => c.Name);

Example 2:

   //USING && INSTEAD OF 2 WHERE CLAUSES
    var companiesData = _dbContext.Companies
         .IncludeFilter(comp => comp.CommunicationLinks
                    .Where(cmli =>
                        string.Equals(cmli.Communication.Action, "PhoneOut")
                        && cmli.UserId == comp.AccountManager 
                    )
                    .Select(comm => comm) //I believe this is where the problem lies
          )
          .Where(co =>
               string.Equals(co.Type, "Customer")
               && string.Equals(co.Status, "Active")
               && co.Deleted != 1 
           )
           .OrderBy(c => c.Name);

I am trying to filter the results to bring back only CommunicationLink records where the child Communication.Action field is "PhoneOut" and Communication.UserId equals the value in the Company.AccountManager (Id). The filtered results are working well, however the Communication object (that I expected to get with the .Select() method) is coming back as null for these records.


Solution

  • After struggling with this for a while this morning I solved it.

    //2 WHERE CLAUSES
    var companiesData = _dbContext.Companies
         .IncludeFilter(comp => comp.CommunicationLinks
                    .Where(cmli =>
                        string.Equals(cmli.Communication.Action, "PhoneOut")
                    )
                    .Where(cmli => cmli.UserId == comp.AccountManager) 
                    .Select(comm => comm.Communication) //forgot Communication!!
          )
          .Where(co =>
               string.Equals(co.Type, "Customer")
               && string.Equals(co.Status, "Active")
               && co.Deleted != 1 
           )
           .OrderBy(c => c.Name);