将递归代码转换为LINQ

时间:2012-05-10 20:01:38

标签: c# linq

我想将此递归调用转换为LINQ,但我不确定如何执行最后两个条件。请告知如何添加最后两个条件。

private void findGoogleOrganic(HtmlNode node)
  {
          if (node.Attributes["class"] != null)
          {
              if (node.Attributes["class"].Value.ToString().Contains("r ld"))
              {
                  String tmp;
                  tmp = node.ParentNode.InnerHtml.ToString();
                  bool condition1 = false;
                  bool condition2 = false;

                  if (tmp != null)
                  {
                      **condition1 = tmp.Contains("qcp47e");
                      condition2 = tmp.Contains("r ld");**
                  }

                  **if (condition1 == false && condition2 == true)**
                  { 
                      GoogleOrganicResults.Add(new Result(URLGoogleOrganic, Listing, node, SearchEngine.Google, SearchType.Organic, ResultType.Website));

                  }
              }
            }

              if (node.HasChildNodes)
              {
                  foreach (HtmlNode children in node.ChildNodes)
                  {
                      findGoogleOrganic(children);
                  }
              }
          }

这是我没有最后两个条件的第一次尝试:

  private void findGoogleOrganicLINQ(HtmlNode node)
  {
      var results = node.Descendants()
             .Where(x => x.Attributes["class"] != null &&
                        x.Attributes["class"].Value.Contains("r ld"))
             .Select(x => new Result(URLGoogleLocal, Listing, x, SearchEngine.Google, SearchType.Local, ResultType.GooglePlaces));

      foreach (Result x in results)
      {
          GoogleOrganicResults.Add(x);
      }
  }

1 个答案:

答案 0 :(得分:0)

if(node.HasChildNodes)
{
     node.ChildNodes.ForEach(findGoogleOrganic);
}