经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
ASP.NET MVC获取多级类别组合下的产品
来源:jb51  时间:2022/9/15 9:09:24  对本文有异议

本篇是针对我在做项目过程中遇到的特定需求而做的一个Demo, 没有很大的通用性,读者酌情可绕行。

标题不能完全表达本意,确切的情景需要展开说。假设有三级分类,关于分类这样设计:

  1. public class Category
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public int ParentId { get; set; }
  6. }

然后产品可以属于多个分类,以下的Categories属性值是以英文逗号隔开、由分类编号拼接而成的字符串。

  1. public class Product
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public string Categories { get; set; }
  6. }

由于种种原因,Categories属性值只是存储了由第三级分类编号拼接而成的字符串。

在前端,需要把分类作为查询条件来查询产品,可能只选择一级分类,把一个数字字符串(比如"1")发送给服务端;可能同时选择一级和二级分类,也把一个数字字符串(比如"1,2")发送给服务端;当然,也有可能同时选择一级、二级和三级分类作为查询条件(比如"1,2,3")。换句话说,如果诸如"1"或"1,2"或"1,2,3"这样的查询条件转换成数组后,如果数组的每一个元素都被包含在Product的Categories属性值转换成的数组中,那这个产品就符合搜索条件。

简单来说,是这样:假设搜索条件是"1,2",Product的Categories属性值为"1,3,2,5",我们不是判断"1,2"这个字符串是否包含在"1,3,2,5"字符串中,而是把"1,2"先split成数组,叫做array1, 把"1,3,2,5"也split成数组,叫做array2,最后判断array1的每个元素是否都被包含在array2中。

还有一个问题需要解决:当前的Product的Categories属性值只存储了所有第三级分类编号拼接成的字符串,而前端输入的搜索条件可能会包含一级分类或二级分类等,所以,我们需要把Product转换一下,希望有一个类的某个属性值能存储由一级、二级、三级分类拼接而成的字符串。

  1. public class ProductWithThreeCate
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public string AllCategoreis { get; set; }
  6. }

以上, AllCategoreis属性值就用来存储由一级、二级、三级分类拼接而成的字符串。

有一个方法获取所有分类:

  1. static List<Category> GetCategories()
  2. {
  3. return new List<Category>()
  4. {
  5. new Category(){Id = 1, Name = "根", ParentId = -1},
  6. new Category(){Id = 2, Name = "一级分类1",ParentId = 1},
  7. new Category(){Id = 3, Name = "一级分类2", ParentId = 1},
  8. new Category(){Id = 4, Name = "二级分类11",ParentId = 2},
  9. new Category(){Id = 5, Name = "二级分类12",ParentId = 2},
  10. new Category(){Id = 6, Name = "二级分类21",ParentId = 3},
  11. new Category(){Id = 7, Name = "二级分类22",ParentId = 3},
  12. new Category(){Id = 8, Name = "三级分类111",ParentId = 4},
  13. new Category(){Id = 9, Name = "三级分类112",ParentId = 4},
  14. new Category(){Id = 10, Name = "三级分类121",ParentId = 5},
  15. new Category(){Id = 11, Name = "三级分类122",ParentId = 5},
  16. new Category(){Id = 12, Name = "三级分类211",ParentId = 6},
  17. new Category(){Id = 13, Name = "三级分类212",ParentId = 6},
  18. new Category(){Id = 14, Name = "三级分类221",ParentId = 7}
  19. };
  20. }

有一个方法获取所有产品:

  1. static List<Product> GetProducts()
  2. {
  3. return new List<Product>()
  4. {
  5. new Product(){Id = 1, Name = "产品1",Categories = "10,12"},
  6. new Product(){Id = 2, Name = "产品2", Categories = "12,13"},
  7. new Product(){Id = 3, Name = "产品3",Categories = "10,11,12"},
  8. new Product(){Id = 4, Name = "产品4",Categories = "13,14"},
  9. new Product(){Id = 5, Name = "产品5",Categories = "11,13,14"}
  10. };
  11. }

接下来的方法是根据搜索条件(比如是"1,2")来查找满足条件的ProductWithThreeCate集合,如下:

  1. /// <summary>
  2. /// 获取满足某些条件的集合
  3. /// </summary>
  4. /// <param name="query">以英文逗号隔开的字符串,比如:2,5</param>
  5. /// <returns></returns>
  6. static List<ProductWithThreeCate> GetResultByQuery(string query)
  7. {
  8. //最终结果
  9. List<ProductWithThreeCate> result = new List<ProductWithThreeCate>();
  10. //临时结果 此时ProductWithThreeCat的属性AllCategoreis包含所有一级、二级、三级分类ID拼接成的字符串
  11. List<ProductWithThreeCate> tempResult = new List<ProductWithThreeCate>();
  12. //获取所有的产品
  13. List<Product> allProducts = GetProducts();
  14. //遍历这些产品
  15. foreach (var item in allProducts)
  16. {
  17. ProductWithThreeCate productWithThreeCate = new ProductWithThreeCate();
  18. productWithThreeCate.Id = item.Id;
  19. productWithThreeCate.Name = item.Name;
  20. //所有一级、二级、三级拼接成以英文逗号隔开的字符串
  21. string temp = string.Empty;
  22. //当前产品只包含三级拼接成的、也是以英文隔开的字符串,split成数组
  23. string[] theThirdCates = item.Categories.Split(',');
  24. //遍历这些三级数组
  25. foreach (string i in theThirdCates)
  26. {
  27. //三级类别转换成整型
  28. int theThirdInt = int.Parse(i);
  29. //获取三级类别
  30. Category theThirdCate = GetCategories().Where(c => c.Id == theThirdInt).FirstOrDefault();
  31. //获取二级类别
  32. Category theSecondCate = GetCategories().Where(c => c.Id == theThirdCate.ParentId).FirstOrDefault();
  33. //获取一级类别
  34. Category theFirstCate = GetCategories().Where(c => c.Id == theSecondCate.ParentId).FirstOrDefault();
  35. temp += i + "," + theSecondCate.Id.ToString() + "," + theFirstCate.Id.ToString() + ",";
  36. }
  37. //去掉最后一个英文逗号
  38. temp = temp.Substring(0, temp.Length - 1);
  39. //转换成集合,去除重复项,比如不同的三级可能有相同的一级或二级父类
  40. IEnumerable<string> tempArray = temp.Split(',').AsEnumerable().Distinct();
  41. //所有一级、二级、三级拼接成以英文逗号隔开的字符串,但已经去除了重复的一级和二级
  42. string tempagain = string.Empty;
  43. //再次遍历集合拼接成字符串
  44. foreach (var s in tempArray)
  45. {
  46. tempagain += s + ",";
  47. }
  48. productWithThreeCate.AllCategoreis = tempagain.Substring(0, tempagain.Length - 1);
  49. tempResult.Add(productWithThreeCate);
  50. }
  51. //遍历临时结果
  52. foreach (var item in tempResult)
  53. {
  54. //把当前包含一级、二级、三级的,以英文逗号隔开的字符串split成数组
  55. string[] itemArray = item.AllCategoreis.Split(',');
  56. //把当前查询字符串split成数组
  57. string[] queryArray = query.Split(',');
  58. //如果queryArray的每一个元素都被包含在itemArray中,那就保存起来
  59. if (queryArray.All(x => itemArray.Contains(x)) == true)
  60. {
  61. result.Add(item);
  62. }
  63. }
  64. return result;
  65. }

客户端的调用如下:

  1. List<ProductWithThreeCate> result = GetResultByQuery("2,5");
  2. //遍历最终的结果
  3. foreach (var item in result)
  4. {
  5. Console.WriteLine(item.Name+ " " + item.AllCategoreis);
  6. }
  7. Console.ReadKey();

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。如果你想了解更多相关内容请查看下面相关链接

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号