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

2022-10-07,,,,

本篇是针对我在做项目过程中遇到的特定需求而做的一个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<category> getcategories()
        {
            return new list<category>()
            {
                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<product> getproducts()
        {
            return new list<product>()
            {
                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集合,如下:

        /// <summary>
        /// 获取满足某些条件的集合
        /// </summary>
        /// <param name="query">以英文逗号隔开的字符串,比如:2,5</param>
        /// <returns></returns>
        static list<productwiththreecate> getresultbyquery(string query)
        {
            //最终结果
            list<productwiththreecate> result = new list<productwiththreecate>();
            //临时结果 此时productwiththreecat的属性allcategoreis包含所有一级、二级、三级分类id拼接成的字符串
            list<productwiththreecate> tempresult = new list<productwiththreecate>();
            //获取所有的产品
            list<product> 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<string> 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<productwiththreecate> result = getresultbyquery("2,5");
            //遍历最终的结果
            foreach (var item in result)
            {
                console.writeline(item.name+ "  " + item.allcategoreis);
            }
            console.readkey(); 

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

《ASP.NET MVC获取多级类别组合下的产品.doc》

下载本文的Word格式文档,以方便收藏与打印。