简体   繁体   中英

Select in Include Repository Pattern Returning null

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 .

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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM