Get All Children to One List - Recursive C#

Get All Children to One List - Recursive C#

You can do SelectMany

List<Location> result = myLocationList.SelectMany(x => x.Children).ToList();

You can use where condition for some selective results like

List<Location> result = myLocationList.Where(y => y.ParentID == someValue)
.SelectMany(x => x.Children).ToList();

If you only required Id's of Children you can do

List<long> idResult = myLocationList.SelectMany(x => x.Children)
.SelectMany(x => x.ID).ToList();

Recursive linq to get infinite children

can you try this..

 public IEnumerable<Location> GetChild(int id)
{
DBEntities db = new DBEntities();
var locations = db.Locations.Where(x => x.ParentLocationID == id || x.LocationID == id).ToList();

var child = locations.AsEnumerable().Union(
db.Locations.AsEnumerable().Where(x => x.ParentLocationID == id).SelectMany(y => GetChild(y.LocationId))).ToList();
return child;
}

How to recursively get all the parents from a child c#?

Something like this

// create Dictionary for lookups
var lookup = dataFromRequest.ToDictionary(x => x.Id.Value, x => x);

var lookFor = new TreeViewNode()
{
Id = 6,
Name = "Child 1",
ParentId = 4
};

// get all the parents
GetParents(lookup, lookFor);

Helper method to get all parents

private void GetParents(Dictionary<int, TreeViewNode> lookup, 
TreeViewNode lookFor)
{
while (lookFor != null)
{
// Alternative: Add lookFor to List<TreeViewNode> here and return at
// the end of the method
Debug.WriteLine($"{lookFor.Id} {lookFor.Name} {lookFor.ParentId}");

if (lookFor.ParentId == null)
break;

// cast ParentId to corrent dataType here Guid or int
lookup.TryGetValue((int)lookFor.ParentId, out var parentNode);
lookFor = parentNode;
}

}

Remember to keep your lookup Dict up to date when you get more IEnumerable<TreeViewNode>

Another thing you could do is set the parent node inside the TreeViewNode like this:

public class TreeViewNode {
...
// set ParentNode by lookup in the Dictionary once
public TreeViewNode ParentNode {get; set;}
}

Get all children of same object by Entity Framework

Okay, there are two solutions that I could find. The first is using the recursive method on the server-side. But in this variant, you need each time request database for each level of hierarchy. The best solution is using Recursion in SQL. You need to do only one request to the database. I attach the link. https://medium.com/swlh/recursion-in-sql-explained-graphically-679f6a0f143b

How to get all children of object recursive with level set to go through?

I made a simple script for you.

Invoke IterateOverChild method with target transform, 0 as current level param and desired depth.

  public class TranformIterator : MonoBehaviour
{
void Start()
{
IterateOverChild(transform, 0, 2);
}

private void IterateOverChild(Transform original, int currentLevel, int maxLevel)
{
if (currentLevel > maxLevel) return;
for (var i = 0; i < original.childCount; i++)
{
Debug.Log($"{original.GetChild(i)}"); //Do with child what you need
IterateOverChild(original.GetChild(i), currentLevel + 1, maxLevel);
}
}
}

Read all nodes and child nodes recursively

You need a recursive function for that. Children can be parents too. If a child has no children beneath it, then we don't add it to the parents dict.

void GetNode(Node parent)
{
if (parent.ChildNodes.Any())
{
ParentN.Add(parent.Name, parent.Path);
foreach(child in parent.ChildNodes)
{
childN.Add(child.Name, child.Path);
GetNode(child);
}
}
Console.WriteLine(parent.Name);
}

Use recursion to get parent value and children value and all its children's children value

To get you moving (and hopefully editing your question with what you've tried so far), you want something like this:

// defined at class level/scope outside method
private List<block> blocks;

...

private int SumAll(int id) {
var initialBlock = blocks.FirstOrDefault(b => b.id == id);
int value = initialBlock.Value;

var childBlocks = blocks.Where(b => b.parentId = id).ToList();
foreach (var childBlock in childBlocks) {
// recursive call for children
value += SumAll(childBlock.id);
}

return value;
}

Include parents in filtered recursive list

You can add function Filter or use appropriate name inside @code like below. And update <CascadingValue Value="Filter(SearchTerm)"> Explanation is in comment.

<CascadingValue Value="Filter(SearchTerm)">
<ItemList Parent="0"/>
</CascadingValue>

public Item[] Filter(string SearchTerm)
{
// Get filtered list. Return all values when SearchTerm is null or empty else return matching values only
var a = Items.Where(i => string.IsNullOrEmpty(SearchTerm) || i.Name.Contains(SearchTerm)).ToList();

// NOTE : Add children first then only search for parent
// Use condition to find all children hierarchy of any object which is not exist in the list
while (Items.Any(i => a.Any(x => x.Id == i.Parent) && !a.Any(x => x.Id == i.Id)))
{
// Add complete children hierarchy for selected iterms
a.AddRange(Items.Where(i => a.Any(x => x.Id == i.Parent) && !a.Any(x => x.Id == i.Id)));
}

// Use condition to find parent of any object which is not exist in the list
while (Items.Any(i => a.Any(x => x.Parent == i.Id) && !a.Any(x => x.Id == i.Id)))
{
// Add all parents who are not already added in list
a.AddRange(Items.Where(i => a.Any(x => x.Parent == i.Id) && !a.Any(x => x.Id == i.Id)));
}

// return final object
return a.ToArray();
}

Aggregating totals using recursion - works one parent-child level only

Your issue seems to be that you aren't actually storing and totalling the product of the recursions.

The fixed method should look something like this:

private int AddTotal(GeoSchemeTreeNode n)
{
int total = n.Maps.Count;

foreach (GeoSchemeTreeNode i in n.Nodes)
{
total += AddTotal(i);
}

return total;
}


Related Topics



Leave a reply



Submit