溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ASP.NET?MVC如何獲取多級類別組合下的產(chǎn)品

發(fā)布時間:2022-09-14 09:33:15 來源:億速云 閱讀:138 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細介紹“ASP.NET MVC如何獲取多級類別組合下的產(chǎn)品”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“ASP.NET MVC如何獲取多級類別組合下的產(chǎn)品”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

假設(shè)有三級分類,關(guān)于分類這樣設(shè)計:

    public class Category
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int ParentId { get; set; }
    }

然后產(chǎn)品可以屬于多個分類,以下的Categories屬性值是以英文逗號隔開、由分類編號拼接而成的字符串。

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Categories { get; set; }
    }

由于種種原因,Categories屬性值只是存儲了由第三級分類編號拼接而成的字符串。

在前端,需要把分類作為查詢條件來查詢產(chǎn)品,可能只選擇一級分類,把一個數(shù)字字符串(比如"1")發(fā)送給服務(wù)端;可能同時選擇一級和二級分類,也把一個數(shù)字字符串(比如"1,2")發(fā)送給服務(wù)端;當然,也有可能同時選擇一級、二級和三級分類作為查詢條件(比如"1,2,3")。換句話說,如果諸如"1"或"1,2"或"1,2,3"這樣的查詢條件轉(zhuǎn)換成數(shù)組后,如果數(shù)組的每一個元素都被包含在Product的Categories屬性值轉(zhuǎn)換成的數(shù)組中,那這個產(chǎn)品就符合搜索條件。

簡單來說,是這樣:假設(shè)搜索條件是"1,2",Product的Categories屬性值為"1,3,2,5",我們不是判斷"1,2"這個字符串是否包含在"1,3,2,5"字符串中,而是把"1,2"先split成數(shù)組,叫做array1, 把"1,3,2,5"也split成數(shù)組,叫做array2,最后判斷array1的每個元素是否都被包含在array2中。

還有一個問題需要解決:當前的Product的Categories屬性值只存儲了所有第三級分類編號拼接成的字符串,而前端輸入的搜索條件可能會包含一級分類或二級分類等,所以,我們需要把Product轉(zhuǎn)換一下,希望有一個類的某個屬性值能存儲由一級、二級、三級分類拼接而成的字符串。

    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}
            };
        }

有一個方法獲取所有產(chǎn)品:

        static List<Product> GetProducts()
        {
            return new List<Product>()
            {
                new Product(){Id = 1, Name = "產(chǎn)品1",Categories = "10,12"},
                new Product(){Id = 2, Name = "產(chǎn)品2", Categories = "12,13"},
                new Product(){Id = 3, Name = "產(chǎn)品3",Categories = "10,11,12"},
                new Product(){Id = 4, Name = "產(chǎn)品4",Categories = "13,14"},
                new Product(){Id = 5, Name = "產(chǎn)品5",Categories = "11,13,14"}
            };
        }

接下來的方法是根據(jù)搜索條件(比如是"1,2")來查找滿足條件的ProductWithThreeCate集合,如下:

        /// <summary>
        /// 獲取滿足某些條件的集合
        /// </summary>
        /// <param name="query">以英文逗號隔開的字符串,比如:2,5</param>
        /// <returns></returns>
        static List<ProductWithThreeCate> GetResultByQuery(string query)
        {
            //最終結(jié)果
            List<ProductWithThreeCate> result = new List<ProductWithThreeCate>();
            //臨時結(jié)果 此時ProductWithThreeCat的屬性AllCategoreis包含所有一級、二級、三級分類ID拼接成的字符串
            List<ProductWithThreeCate> tempResult = new List<ProductWithThreeCate>();
            //獲取所有的產(chǎn)品
            List<Product> allProducts = GetProducts();
            //遍歷這些產(chǎn)品
            foreach (var item in allProducts)
            {
                ProductWithThreeCate productWithThreeCate = new ProductWithThreeCate();
                productWithThreeCate.Id = item.Id;
                productWithThreeCate.Name = item.Name;
                //所有一級、二級、三級拼接成以英文逗號隔開的字符串
                string temp = string.Empty;
                //當前產(chǎn)品只包含三級拼接成的、也是以英文隔開的字符串,split成數(shù)組
                string[] theThirdCates = item.Categories.Split(',');
                //遍歷這些三級數(shù)組
                foreach (string i in theThirdCates)
                {
                    //三級類別轉(zhuǎn)換成整型
                    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);
                //轉(zhuǎn)換成集合,去除重復(fù)項,比如不同的三級可能有相同的一級或二級父類
                IEnumerable<string> tempArray = temp.Split(',').AsEnumerable().Distinct();
                //所有一級、二級、三級拼接成以英文逗號隔開的字符串,但已經(jīng)去除了重復(fù)的一級和二級
                string tempagain = string.Empty;
                //再次遍歷集合拼接成字符串
                foreach (var s in tempArray)
                {
                    tempagain += s + ",";
                }
                productWithThreeCate.AllCategoreis = tempagain.Substring(0, tempagain.Length - 1);
                tempResult.Add(productWithThreeCate);
            }
            //遍歷臨時結(jié)果
            foreach (var item in tempResult)
            {
                //把當前包含一級、二級、三級的,以英文逗號隔開的字符串split成數(shù)組
                string[] itemArray = item.AllCategoreis.Split(',');
                //把當前查詢字符串split成數(shù)組
                string[] queryArray = query.Split(',');
                //如果queryArray的每一個元素都被包含在itemArray中,那就保存起來
                if (queryArray.All(x => itemArray.Contains(x)) == true)
                {
                    result.Add(item);
                }
            }
            return result;
        }

客戶端的調(diào)用如下:

            List<ProductWithThreeCate> result = GetResultByQuery("2,5");
            //遍歷最終的結(jié)果
            foreach (var item in result)
            {
                Console.WriteLine(item.Name+ "  " + item.AllCategoreis);
            }
            Console.ReadKey();

讀到這里,這篇“ASP.NET MVC如何獲取多級類別組合下的產(chǎn)品”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI