I have a large backend DB, which has the following class structure that I'm using:
public class InputClass
{
public int id { get; set; }
public string text { get; set; }
public string icon { get; set; }
public int? parentId { get; set; }
}
As you can see, each element can have a parent ID, which in the end generates a tree-style list of information that the user can interact with.
Using the following sample data as an example:
var inputList = new List<InputClass>();
inputList.Add(new InputClass() { id = 1, text = "Item #1"});
inputList.Add(new InputClass() { id = 2, text = "Item #2" });
inputList.Add(new InputClass() { id = 3, text = "Item #3" });
inputList.Add(new InputClass() { id = 4, text = "SubItem #1", parentId = 1 });
inputList.Add(new InputClass() { id = 5, text = "SubItem #2", parentId = 1 });
inputList.Add(new InputClass() { id = 6, text = "SubItem #3", parentId = 2 });
inputList.Add(new InputClass() { id = 7, text = "Sub-Sub Item #1", parentId = 4 });
I would like to pass an ID # and retrieve a list of all of the elements marked with that parentID. For example, if I had the ID number of 1, the result should be as follows:
ID Name
4 Subitem #1
5 Subitem #2
7 Sub-Sub Item #1
as you can see, the result should return everything that sits underneath id #1, including item with ID #7 (even though it has a parent ID of 4, item #4's parent is #1).
I hope the above makes sense, any ideas on how to achieve this in LINQ?