ora
ora

Reputation: 163

What is the linq expression for that

I do not know how to refactor this expression.

TreeViewItemNode FindNode(TreeViewItemNode nodeCollection)
{
    foreach (var child in nodeCollection.Children)
        {
            var found = FindNodeContainingVarId(child, varId);
            if (found != null)
                return found;
        }

   return null;
}

Upvotes: 0

Views: 50

Answers (1)

Fruchtzwerg
Fruchtzwerg

Reputation: 11389

First select the node and take the first found node or null if no node was found like:

TreeViewItemNode FindNode(TreeViewItemNode nodeCollection)
{
    return nodeCollection.Children
        .Select(child => FindNodeContainingVarId(child, varId).
        .FirstOrDefault(node => node != null);
}

Upvotes: 1

Related Questions