1

我想将此递归调用转换为 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);
      }
  }
4

1 回答 1

0
if(node.HasChildNodes)
{
     node.ChildNodes.ForEach(findGoogleOrganic);
}
于 2012-05-10T20:09:55.250 回答