Jaime
Jaime

Reputation: 143

How can I compact this code with a Linq expression?

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;
    }

Upvotes: 1

Views: 56

Answers (1)

wertzui
wertzui

Reputation: 5730

You can condense it further down using just one expression bodied method.

public bool AllChildRowsExpanded()
    => IsExpanded && (ContainedRows == null || ContainedRows.All(r => r.AllChildRowsExpanded()));

Upvotes: 1

Related Questions