2
I have a select that returns chained data and plays it in a Viewmodel created for the simple reason that I cannot return all data from 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 it is possible to notice by Includes for example: Formsectionfields. The problem occurs precisely in the return of this "third level" that comes empty, even with the includes. Also occurs for any collection that is below the collection f. Formsection.
Some indication of what to do or what might 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), as it is necessary that only one row be returned with the chained objects.
"[...] a Join could make the whole routine extremely slow [...]". The
Includes
are translated into joins.– Jéf Bueno
In this case I meant joins using LINQ (query syntax), which would change the way the data is brought from the database. Changing the question to avoid confusion.
– flvrm92
Do you guarantee me that your query should bring some record that has relationship with this third level? include is translated to INNER JOIN, nay FULL JOIN. Do the following, copy the query generated in Iqueryable and try to run directly in the database and see the result.
– Gabriel Coletta
@Gabrielcoletta copying the query generated in Iqueryable the select runs only in the first table, with no Join
– flvrm92
You can share with the community?
– Gabriel Coletta
@Gabrielcoletta managed to solve the problem using the answer of Marconcilio Souza. Thank you
– flvrm92