I have this class:
public class Division
{
public int Id { get; set; }
public string Naam { get; set; }
public ICollection<Division> Children { get; set; }
}
Example of the filled object/list:
Id 1
Name "HQ"
Children
0
Id 200
Name "HR"
Children
0
Id 800
Name "Payrolls"
Children
0
Id 1001
Name "Years"
Children
1
Id 1002
Name "Level"
Children
1
Id 900
Name "Functions"
Children
0
Id 2000
Naam "Grades"
Children
...
Each item can have many nested 'Children'. Now I want to find an item by Id, how can I achieve this?
I tried to put the result into a list.
lstDivision = Division.Children.ToList();
and find the item by:
Division d = lstDivision.SelectMany(d => d.Children).Where(c => c.Id==2000).FirstOrDefault();
The result is null.