Home > Back-end >  Multiple levels with theninclude not returning all rows unless I query the detail
Multiple levels with theninclude not returning all rows unless I query the detail

Time:01-22

I'm using .net core 2.0 (I know) and I've got nested data I'm pulling from sql.

The weird thing is that some of the data isn't showing up unless I explicitly search for it.

var record = db.Records
               .Include(x => x.ParentLabels)
               .Include(x => x.TopChildren)
               .ThenInclude(x => x.LevelTwoChildren)
               .ThenInclude(x => x.LevelThreeChildren)
               .ThenInclude(x => x.LevelTwoChildren.LevelTwoLabels)
               .FirstOrDefault(x => x.Id.Equals(id));

The weird thing is that some of the LevelTwoChildren.Labels data does not show up, UNLESS I add the following:

var t = record.TopChildren.FirstOrDefault();
var tt = t.LevelTwoChildren.FirstOrDefault(x => x.Id.Equals(id2));

I've tried doing a foreach through all the record levels but that doesn't help.

Any ideas?

CodePudding user response:

I guess it is happening because of .ThenInclude(x => x.LevelTwoChildren.LevelTwoLabels). Include statements should include one property at a time. What you should do is:

var record = db.Records
              .Include(x => x.ParentLabels)
              .Include(x => x.TopChildren)
                .ThenInclude(x => x.LevelTwoChildren)
                  .ThenInclude(x => x.LevelThreeChildren)
              .Include(x => x.TopChildren)
                .ThenInclude(x => x.LevelTwoChildren)
                  .ThenInclude(x => x.LevelTwoLabels)
              .FirstOrDefault(x => x.Id.Equals(id));
  •  Tags:  
  • Related