private static IEnumerable <HtmlNode> ExecuteSelectorRecursively(IEnumerable <ManagedQueryPart> queryParts, HtmlNodeCollection items) { // abort condition if (items.NullOrNone() || queryParts.NullOrNone()) { yield break; } foreach (var shallowItem in items) { if (FindMatch(shallowItem, queryParts.First())) { if (queryParts.Count() == 1) { yield return(shallowItem); } else { foreach (var deepItem in ExecuteSelectorRecursively(queryParts.Skip(1), shallowItem.ChildNodes)) { yield return(deepItem); } } } foreach (var deepItem in ExecuteSelectorRecursively(queryParts, shallowItem.ChildNodes)) { yield return(deepItem); } } }