I have a select that returns chained data and plays them in a ViewModel created for the simple reason that I can not return all the data in my table, as in the example:
var data = _context.Forms
.Include(i => i.OrganizationUnit)
.Include(i => i.FormSections)
.Include(i => i.FormSections.Select(p => p.Predecessors))
.Include(i => i.FormSections.Select(p => p.FormSectionSecurities))
.Include(i => i.FormSections.Select(p => p.FormSectionSecurities.Select(x => x.FormSectionSecurityPremission)))
.Include(i => i.FormSections.Select(p => p.FormSectionSecurities.Select(x => x.Role)))
.Include(i => i.FormSections.Select(p => p.FormSectionFields))
.Include(i => i.FormSections.Select(p => p.FormSectionFields.Select(x => x.FormSectionFieldType)))
.Include(i => i.FormSections.Select(p => p.FormSectionFields.Select(x => x.FormSectionFieldType)))
.Include(i => i.FormSections.Select(p => p.FormSectionFields.Select(x => x.List)))
.Include(i => i.FormSections.Select(p => p.FormSectionFields.Select(x => x.List).Select(j => j.ListItems)))
.OrderBy(i => i.Name)
.AsNoTracking()
.Where(i => i.EFormStatus == EFormStatus.Active)
.Select(f => new FormViewModel
{
Id = f.Id,
Name = f.Name,
Description = f.Description,
EFormStatus = f.EFormStatus,
Instructions = string.Empty,
OrganizationUnit = f.OrganizationUnit,
FormSections = f.FormSections
}).ToList();
f.FormSection returns a collection that has more collections inside, as you can see for Includes , for example: FormSectionFields . The problem occurs precisely in the return of this "third level" that comes empty, even with the includes. It also occurs for any collection that is below the f.FormSection collection.
Any indication of what to do or what may be wrong?
Below FormViewModel
public class FormViewModel
{
public Guid Id { get; set; }
public string Name { get; set; }
public OrganizationUnit OrganizationUnit { get; set; }
public string Description { get; set; }
public string Instructions { get; set; }
public EFormStatus EFormStatus { get; set; }
public ICollection FormSections { get; set; }
}
UPDATED
I discard the use of joins using Linq (query syntax), since it is necessary that only one row is returned with the chained objects.