1

I have a database Model that looks like this:

  1. Complaints (T1)
  2. MonitoringComplaints(T2)
  3. Monitoring Notes (T3)

My Class for each looks as below:

class Complaints{
//props

    public virtual ICollection<MONITORING> MONITORINGs { get; set; }
}


class Monitoring{
//props

    public virtual ICollection<MonitoringNotes> MONITORINGNotes { get; set; }
}

class MonitoringNotes{

//props

}

I get all my data when I run the repository to get all as bellow

    public IEnumerable<Complaints> GetAll()
    {

        try
        {
            return _context.Complaints

                .Include(t => t.MONITORINGs)
                .OrderBy(t => t.FileNum)
                .ToList();
        }
        catch (Exception ex)
        {
            _logger.LogError("Could not get complaint with checklist", ex);
            return null;
        }
    }

but when I add the notes to the monitoring with the select, it returns Null:

public IEnumerable<Complaints> GetAll()
    {

        try
        {
            return _context.Complaints

                .Include(t => t.MONITORINGs.Select(t3=>t3.MONITORINGNotes )
                .OrderBy(t => t.FileNum)
                .ToList();
        }
        catch (Exception ex)
        {
            _logger.LogError("Could not get complaint with checklist", ex);
            return null;
        }
    }

Also when I type in select, I get the Intellisense for the notes, so I don't think its my entities. Can someone point me into the right direction if I am using the select statement correctly? I used this solution on many of the questions asked about including the level 3 relationships, like this one: Entity Framework - Include Multiple Levels of Properties.

Community
  • 1
  • 1
enavuio
  • 1,428
  • 2
  • 19
  • 31
  • Does `Monitoring` already have any `MonitoringNotes` ? Because when there are no items, the property will be null. Just checking... – Chris Mar 26 '16 at 02:58
  • @Chris thank you for checking. It does. Some of the complaints don't have any monitoring data but the complaints still show up. After including the select statement, it just returns null. – enavuio Mar 26 '16 at 03:04
  • I ran you code and it worked as expected. And btw, there is a `)`missing at the end of the `.Include(..Select()` line. Do you have any special EF config for you relations ? I ran with no config (by convention). – Chris Mar 26 '16 at 03:18
  • @Chris okay. Let me check my entity patterns. Thank you for checking. – enavuio Mar 26 '16 at 03:19
  • Here is what I ran: http://pastebin.com/raw/hiP1Eg0r and the output http://pastebin.com/raw/TSN2Ge3e – Chris Mar 26 '16 at 03:23
  • @Chris I do not have any special configs just the way you ran with no configs and just the relationships built within the sql server database. It probably is my setup. I won't get back to work till weds. Can I share with you what I find weds? – enavuio Mar 26 '16 at 03:30
  • Sure, enjoy the long weekend ;-) – Chris Mar 26 '16 at 03:32
  • @Chris have a great weekend! I may try this again on my personal computer and let you know. – enavuio Mar 26 '16 at 03:52
  • @Chris, after some debugging, I have used ThenInclude and that works. – enavuio Mar 31 '16 at 16:05
  • @Chris, could I ask you for some assistance on including a "customer" from a table that has a many to one relationship to the "customer"'s table? I am easily able to comprehend the one to many method but when I setup my entities to for the customer table, I don't have a IEnumerable of an officer. I haven't found a clear example of doing that. – enavuio Mar 31 '16 at 17:08

1 Answers1

1

Using then include solved my problem.

public IEnumerable<Complaints> GetAll()
{

    try
    {
        return _context.Complaints

            .Include(t => t.MONITORINGs)
                    .ThenInclude(t3=>t3.MONITORINGNotes )
            .OrderBy(t => t.FileNum)
            .ToList();
    }
    catch (Exception ex)
    {
        _logger.LogError("Could not get complaint with checklist", ex);
        return null;
    }
}
enavuio
  • 1,428
  • 2
  • 19
  • 31