I have this code for checking if the all the descendant nodes of a node are expanded or not. How can I compact this code with a Linq expression? I understand how the code works but I'm not being able to do the same thing in Linq.
public bool AllChildRowsExpanded()
{
if (!this.IsExpanded)
{
return false;
}
foreach (var row in this.ContainedRows)
{
if (row.ContainedRows == null || row.ContainedRows.Count == 0)
{
continue;
}
if (!row.AllChildRowsExpanded())
{
return false;
}
}
return true;
}
CodePudding user response:
I have little to no insight into what kind of circumstances we're operating on here but something along these lines could potentially work.
public bool AllChildRowsExpanded(){
//Perhaps we also want to control that
//we actually have children here aswell?
if(!this.IsExpanded)
{
return false
}
if(this.ContainedRows == null || this.ContainedRows.Count == 0)
{
return true
}
return this.ContainedRows.Any(row => !AllChildRowsExpanded())
}
CodePudding user response:
You can condense it further down using just one expression bodied method.
public bool AllChildRowsExpanded()
=> IsExpanded && (ContainedRows == null || ContainedRows.All(r => r.AllChildRowsAreExpanded()))