I am trying to get a complete tree in a single loop using an iterative function. For instance, this is what I have:
var someVariable = GetAllChildPositions(posId) //Method called from a console method.
public ICollection<Position> GetAllChildPositions(int posId)
{
ICollection<Position> posCollection = new Collection<Position>();
posCollection= GetChildPos(posId,posCollection);
return posCollection;
}
public ICollection<Position> GetChildPos(int posId, ICollection<Position> posCollection)
{
var posObject= serviceRepo.Position.GetById(posId);
posCollection.Add(posObject);
if (serviceRepo.Position.GetAll().Where(x => x.ParentPosId == posId).Any())
{
var childPositions = serviceRepo.Position().GetAll().Where(x => x.ParentPosId == posId);
foreach (var childPos in childPositions )
{
GetChildRows(childPos.Id, posCollection);
}
}
return posCollection;
}
I am trying to get a list/collection child positions for a given parent position Id. This method works quite well and gets the job done albeit a little slowly. I am interested in knowing if there is a better way of doing this.
DatabsaeCall()
was a clear sign that this was fake code. Thanks for editing; I have reopened the question. \$\endgroup\$Collection<T>
does exist. \$\endgroup\$