Answer the question
In order to leave comments, you need to log in
What's the best algorithm/approach for traversing a tree structure using EntityFramework?
Good day to all. There is the following structure:
I.e. in this form, the data is stored in the database and the same tree model should come to the user on the UI. Accordingly, the goal is to go through this structure and create a model based on it.
At the moment, this is done as clumsily as possible: a separate mapper has been created for each entity (table), and it all turns out something like this:
var entity = _entityRepository.Table.FirstOrDefault(x => x.Id == id);
var viewModel = _entityBuilder.BuildViewModel(entity);
var model = new ViewModel();
model.ChildEntitiesA = entity.ChildEntitiesA.Select(childEntityA => childEntityABuilder.BuildViewModel(childEntityA));
///childEntityABuilder
modelChildEntityA.ChildEntitiesB = childEntityA.ChildEntitiesB.Select(childEntityB => childEntityBBuilder.BuildViewModel(childEntityB));
foreach(var childEntityA in entity.childEntitiesA){
foreach(var childEntityB in childEntityA.ChildEntitiesB){
foreach(var childEntityC in childEntityB.ChildEntitiesC)
{
//..и тд и тп.
}
}
}
Answer the question
In order to leave comments, you need to log in
Try this approach
https://entityframework.net/knowledge-base/4853797...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question