搜索

查看: 3084|回复: 11

[ASP.NET] ASP.NET MVC获取多级类别组合下的产品

[复制链接]
发表于 2023-5-4 11:34:12 | 显示全部楼层 |阅读模式
Editor 2023-5-4 11:34:12 3084 11 看全部
本篇是针对我在做项目过程中遇到的特定需求而做的一个Demo, 没有很大的通用性,读者酌情可绕行。
标题不能完全表达本意,确切的情景需要展开说。假设有三级分类,关于分类这样设计:
    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int ParentId { get; set; }
    }
然后产品可以属于多个分类,以下的Categories属性值是以英文逗号隔开、由分类编号拼接而成的字符串。
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Categories { get; set; }
    }
由于种种原因,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转换一下,希望有一个类的某个属性值能存储由一级、二级、三级分类拼接而成的字符串。
    public class ProductWithThreeCate
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string AllCategoreis { get; set; }
    }
以上, AllCategoreis属性值就用来存储由一级、二级、三级分类拼接而成的字符串。
有一个方法获取所有分类:
        static List GetCategories()
        {
            return new List()
            {
                new Category(){Id = 1, Name = "根", ParentId = -1},
                new Category(){Id = 2, Name = "一级分类1",ParentId = 1},
                new Category(){Id = 3, Name = "一级分类2", ParentId = 1},
                new Category(){Id = 4, Name = "二级分类11",ParentId = 2},
                new Category(){Id = 5, Name = "二级分类12",ParentId = 2},
                new Category(){Id = 6, Name = "二级分类21",ParentId = 3},
                new Category(){Id = 7, Name = "二级分类22",ParentId = 3},
                new Category(){Id = 8, Name = "三级分类111",ParentId = 4},
                new Category(){Id = 9, Name = "三级分类112",ParentId = 4},
                new Category(){Id = 10, Name = "三级分类121",ParentId = 5},
                new Category(){Id = 11, Name = "三级分类122",ParentId = 5},
                new Category(){Id = 12, Name = "三级分类211",ParentId = 6},
                new Category(){Id = 13, Name = "三级分类212",ParentId = 6},
                new Category(){Id = 14, Name = "三级分类221",ParentId = 7}
            };
        }
有一个方法获取所有产品:
        static List GetProducts()
        {
            return new List()
            {
                new Product(){Id = 1, Name = "产品1",Categories = "10,12"},
                new Product(){Id = 2, Name = "产品2", Categories = "12,13"},
                new Product(){Id = 3, Name = "产品3",Categories = "10,11,12"},
                new Product(){Id = 4, Name = "产品4",Categories = "13,14"},
                new Product(){Id = 5, Name = "产品5",Categories = "11,13,14"}
            };
        }
接下来的方法是根据搜索条件(比如是"1,2")来查找满足条件的ProductWithThreeCate集合,如下:
        ///
        /// 获取满足某些条件的集合
        ///
        /// 以英文逗号隔开的字符串,比如:2,5
        ///
        static List GetResultByQuery(string query)
        {
            //最终结果
            List result = new List();
            //临时结果 此时ProductWithThreeCat的属性AllCategoreis包含所有一级、二级、三级分类ID拼接成的字符串
            List tempResult = new List();
            //获取所有的产品
            List allProducts = GetProducts();
            //遍历这些产品
            foreach (var item in allProducts)
            {
                ProductWithThreeCate productWithThreeCate = new ProductWithThreeCate();
                productWithThreeCate.Id = item.Id;
                productWithThreeCate.Name = item.Name;
                //所有一级、二级、三级拼接成以英文逗号隔开的字符串
                string temp = string.Empty;
                //当前产品只包含三级拼接成的、也是以英文隔开的字符串,split成数组
                string[] theThirdCates = item.Categories.Split(',');
                //遍历这些三级数组
                foreach (string i in theThirdCates)
                {
                    //三级类别转换成整型
                    int theThirdInt = int.Parse(i);
                    //获取三级类别
                    Category theThirdCate = GetCategories().Where(c => c.Id == theThirdInt).FirstOrDefault();
                    //获取二级类别
                    Category theSecondCate = GetCategories().Where(c => c.Id == theThirdCate.ParentId).FirstOrDefault();
                    //获取一级类别
                    Category theFirstCate = GetCategories().Where(c => c.Id == theSecondCate.ParentId).FirstOrDefault();
                    temp += i + "," + theSecondCate.Id.ToString() + "," + theFirstCate.Id.ToString() + ",";
                }
                //去掉最后一个英文逗号
                temp = temp.Substring(0, temp.Length - 1);
                //转换成集合,去除重复项,比如不同的三级可能有相同的一级或二级父类
                IEnumerable tempArray = temp.Split(',').AsEnumerable().Distinct();
                //所有一级、二级、三级拼接成以英文逗号隔开的字符串,但已经去除了重复的一级和二级
                string tempagain = string.Empty;
                //再次遍历集合拼接成字符串
                foreach (var s in tempArray)
                {
                    tempagain += s + ",";
                }
                productWithThreeCate.AllCategoreis = tempagain.Substring(0, tempagain.Length - 1);
                tempResult.Add(productWithThreeCate);
            }
            //遍历临时结果
            foreach (var item in tempResult)
            {
                //把当前包含一级、二级、三级的,以英文逗号隔开的字符串split成数组
                string[] itemArray = item.AllCategoreis.Split(',');
                //把当前查询字符串split成数组
                string[] queryArray = query.Split(',');
                //如果queryArray的每一个元素都被包含在itemArray中,那就保存起来
                if (queryArray.All(x => itemArray.Contains(x)) == true)
                {
                    result.Add(item);
                }
            }
            return result;
        }               
客户端的调用如下:
            List result = GetResultByQuery("2,5");
            //遍历最终的结果
            foreach (var item in result)
            {
                Console.WriteLine(item.Name+ "  " + item.AllCategoreis);
            }
            Console.ReadKey();
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对知鸟论坛的支持。如果你想了解更多相关内容请查看下面相关链接
回复

使用道具 举报

发表于 2023-6-28 19:58:04 | 显示全部楼层
心随674 2023-6-28 19:58:04 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-6-28 21:26:06 | 显示全部楼层
普通人物怨 2023-6-28 21:26:06 看全部
楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
回复

使用道具 举报

发表于 2023-6-29 01:01:16 | 显示全部楼层
123456809 2023-6-29 01:01:16 看全部
楼主,大恩不言谢了!知鸟论坛是最棒的!
回复

使用道具 举报

发表于 2023-6-29 13:50:12 | 显示全部楼层
123456848 2023-6-29 13:50:12 看全部
我看不错噢 谢谢楼主!知鸟论坛越来越好!
回复

使用道具 举报

发表于 2023-6-29 17:17:10 | 显示全部楼层
计划你大爷计j 2023-6-29 17:17:10 看全部
楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
回复

使用道具 举报

发表于 2023-6-30 03:33:48 | 显示全部楼层
风来时狂放 2023-6-30 03:33:48 看全部
这东西我收了!谢谢楼主!知鸟论坛真好!
回复

使用道具 举报

发表于 2023-6-30 09:25:07 | 显示全部楼层
音乐之家1 2023-6-30 09:25:07 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-7-3 08:43:01 | 显示全部楼层
123456823 2023-7-3 08:43:01 看全部
这东西我收了!谢谢楼主!知鸟论坛真好!
回复

使用道具 举报

发表于 2023-7-4 08:16:18 | 显示全部楼层
当当当当裤裆坦 2023-7-4 08:16:18 看全部
楼主,我太崇拜你了!我想我是一天也不能离开知鸟论坛
回复

使用道具 举报

  • 您可能感兴趣
点击右侧快捷回复 【请勿灌水】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表

RSS订阅| SiteMap| 小黑屋| 知鸟论坛
联系邮箱E-mail:zniao@foxmail.com
快速回复 返回顶部 返回列表