How to search Hierarchical Data with Linq

前端 未结 8 666
心在旅途
心在旅途 2020-12-03 11:43

I need to search a tree for data that could be anywhere in the tree. How can this be done with linq?

class Program
{
    static void Main(string[] args) {

         


        
8条回答
  •  执笔经年
    2020-12-03 12:01

    Well, I guess the way is to go with the technique of working with hierarchical structures:

    1. You need an anchor to make
    2. You need the recursion part

      // Anchor
      
      rootFamily.Children.ForEach(childFamily => 
      {
          if (childFamily.Name.Contains(search))
          {
             // Your logic here
             return;
          }
          SearchForChildren(childFamily);
      });
      
      // Recursion
      
      public void SearchForChildren(Family childFamily)
      {
          childFamily.Children.ForEach(_childFamily => 
          {
              if (_childFamily.Name.Contains(search))
              {
                 // Your logic here
                 return;
              }
              SearchForChildren(_childFamily);
          });
      }
      

提交回复
热议问题