I have a need where I need to loop through sublist of each class.
public Class A
{
public int IdA {get;set;}
public string Name {get;set;}
public List<Class B> Items {get;set;}
}
public Class B
{
public string IdA {get;set;}
public string IdB {get;set;}
public string SecondName {get;set;}
public List<Class C> Items {get;set;}
}
public Class C
{
public string IdB {get;set;}
public string IdC {get;set;}
public string ThirdName {get;set;}
public List<Class D> Items {get;set;}
}
public Class D
{
public string IdC {get;set;}
public string IdD {get;set;}
public string FourthName {get;set;}
}
Class a = new class A {“a1″,”name1”,{{“a1″,”b1″,”name2”}},{“a1″,”b2″,”name3”,{“b2″,”c1″,”name3”}},{}}}
Class A is the main class , It will have a sub list of b and Class B may have sub list of Class C and Class c may have sub list of D.
I wanted to loop through each object of Class A and if it has the sub list of b then loop through it and display each object in the table. If the there are sub list then there will be expand and collapse link.
I am looking for a general function to loop through any class with sub list and display it when the use clicks the link button to see the sub list. I wanted to loop through the hierarchy
foreach(var A in List<A>)
{
Console.WriteLine(a.Name);
foreach(var B in A)
{
Console.WriteLine(b.SecondName)
foreach(var c in B)
{
Console.WriteLine(c.ThirdName)
foreach(var D item in C)
{
Console.WriteLine(b.FourthName )
}
}
}
}
But how do I Know how many inner levels nested?In the following picture Temporary file is the grandprents , c:windowstemp* is the parents and the files under it is the children. the nested file relation.
6
You can use a base object and inheritance to simplify the code and make it more generic. This base object contains the common properties and a list of child items.
public class BaseObject
{
public string Id { get; set; }
public string Name { get; set; }
public List<BaseObject> Items { get; set; } = new List<BaseObject>();
}
Derive specific classes from the base class:
public class A : BaseObject
{
public int IdA
{
get { return int.Parse(Id); }
set { Id = value.ToString(); }
}
public new List<B> Items
{
get { return base.Items.ConvertAll(x => (B)x); }
set { base.Items = value.ConvertAll(x => (BaseObject)x); }
}
}
public class B : BaseObject
{
public string IdA { get; set; }
public string IdB
{
get { return Id; }
set { Id = value; }
}
public new List<C> Items
{
get { return base.Items.ConvertAll(x => (C)x); }
set { base.Items = value.ConvertAll(x => (BaseObject)x); }
}
}
public class C : BaseObject
{
public string IdB { get; set; }
public string IdC
{
get { return Id; }
set { Id = value; }
}
public new List<D> Items
{
get { return base.Items.ConvertAll(x => (D)x); }
set { base.Items = value.ConvertAll(x => (BaseObject)x); }
}
}
public class D : BaseObject
{
public string IdC { get; set; }
public string IdD
{
get { return Id; }
set { Id = value; }
}
}
Then use a recursive method to traverse through the hierarchy:
static void Traverse(BaseObject obj, int level)
{
Console.WriteLine(new string(' ', level * 2) + $"{obj.GetType().Name}: {obj.Id}, {obj.Name}");
foreach (var item in obj.Items)
{
Traverse(item, level + 1);
}
}
}
The recursive traversal function is simpler and can handle any level of nested lists.