I have a list of objects with the following basic structure:
class Person
{
public int ID {get; set;}
public bool ShowChildren {get; set;}
public int ParentID {get; set;}
// ...many other properties...
}
I need to return a list of Person parent classes that are orderd by their ID. If the ShowChildren flag is enabled, also return the children under their parent, ordered by their ID.
This is only one level deep, i.e. children won’t have children.
I can write a linq statement to give me all of the parents, but I’m stuck on how to also include the sorted children when the parent’s flag is enabled.
var People = PersonList
.Where(x => x.ParentID == 0)
.Orderby(x => x.ID)
.ToList();
Sorry, if you only want to return parents unless explicitly requested (thanks, @Rawling!), a
foreachloop is also nice.