【问题标题】:How to find hierarchical tree last nodes for a specific category id c#如何查找特定类别 id c# 的分层树最后一个节点
【发布时间】:2019-12-24 07:53:10
【问题描述】:
        List<Categories> categories = new List<Categories>
        {
            new Categories { CatID = 1, CatName = "Computer, IT & Networking"},
            new Categories { CatID = 2, CatName = "Computer & Servers"},
            new Categories { CatID = 3, CatName = "Desktop PCs"},
            new Categories { CatID = 4, CatName = "Servers"},
            new Categories { CatID = 5, CatName = "Computer Parts & Components"},
            new Categories { CatID = 6, CatName = "Harddrive"},
            new Categories { CatID = 7, CatName = "CPUs"},
            new Categories { CatID = 8, CatName = "Electronics"},
            new Categories { CatID = 9, CatName = "Furniture"},
            new Categories { CatID = 10, CatName = "RAM"},
            new Categories { CatID = 11, CatName = "Harddrive"},
            new Categories { CatID = 12, CatName = "Workbooks"},
            new Categories { CatID = 13, CatName = "IBM Desktop"},
            new Categories { CatID = 14, CatName = "HP Desktop"}
        };

        List<CategoriesMapping> categoriesMapping = new List<CategoriesMapping>
        {
            new CategoriesMapping { CategoriesMappingID = 1, CatID = 1, MapCatID = 2},
            new CategoriesMapping { CategoriesMappingID = 2, CatID = 1, MapCatID = 5},
            new CategoriesMapping { CategoriesMappingID = 3, CatID = 2, MapCatID = 3},
            new CategoriesMapping { CategoriesMappingID = 4, CatID = 2, MapCatID = 4},
            new CategoriesMapping { CategoriesMappingID = 5, CatID = 5, MapCatID = 6},
            new CategoriesMapping { CategoriesMappingID = 6, CatID = 5, MapCatID = 7},
            new CategoriesMapping { CategoriesMappingID = 7, CatID = 5, MapCatID = 10},
            new CategoriesMapping { CategoriesMappingID = 8, CatID = 5, MapCatID = 11},
            new CategoriesMapping { CategoriesMappingID = 9, CatID = 2, MapCatID = 12},
            new CategoriesMapping { CategoriesMappingID = 10, CatID = 3, MapCatID = 13},
            new CategoriesMapping { CategoriesMappingID = 11, CatID = 3, MapCatID = 14}
        };

如何获取特定 CatID 的最后一个节点,例如在上面的代码中,如果我选择 CatID = 2,它应该只带回 CatID 4、12、13、14。它不应该返回其子类别 CatID 3,因为它也有 13 和 14 的节点。相反,它只返回 CatID 3 的子节点。

【问题讨论】:

  • 您的问题很难解读。你应该努力改进它。
  • 不是,我觉得挺容易理解的。
  • 那么到目前为止你尝试过什么?

标签: c# categories


【解决方案1】:

我假设您正在尝试使用 linq 魔法解决此问题。不幸的是,在this answer by Jon Skeet,你可以看到它很不方便。

你可以试试like this,但它会变得相当昂贵。

我的建议是创建一个新函数,例如

   public List<int> GetNoDescendateCategoriesCategoryId(int categoryId) {
       List<int> newCategories = new List<int>();
       List<int> childCategories = this.categoriesMapping.Where(m => m.CatID == categoryId).Select(x=> x.MapCatID).ToList();

       if(childCategories.Count == 0) {
          return new List<int>() {categoryId};
       }
       foreach (int catId in childCategories ) {
          newCategories.AddRange(GetNoDescendateCategoriesCategoryId(catId));
       }
       return newCategories;
   }

你可以查看https://dotnetfiddle.net/1IbjOf这个例子。

【讨论】:

    【解决方案2】:

    由于这是一个递归调用,我不认为 LINQ 是最好的方法。拥有一个简单的迭代或递归函数就可以完成这项工作。

    
        public IEnumerable<int> ExtractSubcategories(int categoryId)
        {
            var categories = new List<int> {categoryId};
            var subCategories = new List<int>();
    
            while (categories.Count > 0)
            {
                // categories will store the categories to check
                int cat = categories[0];
                categories.RemoveAt(0);
    
                // checking the mapped categories
                var mapped = categoriesMapping.Where(x => x.CatID == cat)
                                              .Select(x => x.MapCatID)
                                              .ToList();
                if (mapped.Count > 0) 
                {
                    // there are mapped categories,
                    // thus we look for their children
                    categories.AddRange(mapped);
                }
                else 
                {
                    // there are no mapped categories,
                    // this category is a child, we add it to the result
                    subCategories.Add(cat);
                }
            }
    
            return subCategories;
        }
    
    
        [Test]
        public void Test() => 
            CollectionAssert.AreEquivalent(ExtractSubcategories(2), new[] {4, 12, 13, 14});
    
    

    【讨论】:

      【解决方案3】:

      你可以试试下面的,

      public class A {
      
          public List<int> result = new List<int>();
      
          public static List<CategoriesMapping> categoriesMapping = new List<CategoriesMapping> {
              //your data
          };
      
          public List<int> GetChildren(int input) {
              var res = GetInnerChildren(input);
              if (res.Count == 0)
                  result.Add(input);
      
              foreach (var item in res) {
                  if (categoriesMapping.Count(x => x.CatID == item) == 0) {
                      result.Add(item);
                  }
                  else {
                      var childs = GetInnerChildren(item);
                      foreach (var item2 in childs) {
                          GetChildren(item2);
                      }
                  }
              }
              result.Sort();
              return result;
          }
      
          public List<int> GetInnerChildren(int input) {
              return categoriesMapping.Where(x => x.CatID == input).Select(x => x.MapCatID).ToList() ?? new List<int>();
          }
      
      }
      

      您的 CategoriesMapping 模型,

      public class CategoriesMapping {
          public int CategoriesMappingID { get; set; }
          public int CatID { get; set; }
          public int MapCatID { get; set; }
      }
      

      GetChildren 根据需要返回所需的输出

      GetChildren(2) => 返回 4,12,13,14

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-01
        • 1970-01-01
        • 2020-07-24
        • 2012-04-22
        • 2021-12-20
        • 1970-01-01
        • 1970-01-01
        • 2020-05-06
        相关资源
        最近更新 更多