I have such List<IRequest>
public interface IRequest
{
List<IRequest> Childrens { get; set; }
bool RequestIsSelected { get; set; }
}
How to find RequestIsSelected object?
Let's say I have such structure
IRequest -
|
IRequest -
|
IRequest <--- if this one is true I can catch it
|
IRequest -
|
IRequest <----- This one RequestIsSelected == true; (I can't catch it)
I wrote such method
private IRequest GetSelectedItem(List<IRequest> entireList)
{
foreach(var tmp in entireList)
{
if(tmp.RequestIsSelected)
{
return tmp;
}
else
{
return GetSelectedItem(tmp.Childrens);
}
}
return null;
}
But it iterates only on first deep line, if my selected item resides in second line my method returns null.
What am I doing wrong?
else, onlyreturnif the result of the recursive call is notnull.