{"id":256170,"date":"2022-11-12T08:47:35","date_gmt":"2022-11-12T00:47:35","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=256170"},"modified":"2022-10-28T12:48:09","modified_gmt":"2022-10-28T04:48:09","slug":"school-net-mvc","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/school-net-mvc.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdASP.NET MVC\u83b7\u53d6\u591a\u7ea7\u7c7b\u522b\u7ec4\u5408\u4e0b\u7684\u4ea7\u54c1"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86ASP.NET MVC\u83b7\u53d6\u591a\u7ea7\u7c7b\u522b\u7ec4\u5408\u4e0b\u4ea7\u54c1\u7684\u65b9\u6cd5\uff0c\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\u3002\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u501f\u9274\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u672c\u7bc7\u662f\u9488\u5bf9\u6211\u5728\u505a\u9879\u76ee\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u7279\u5b9a\u9700\u6c42\u800c\u505a\u7684\u4e00\u4e2aDemo, \u6ca1\u6709\u5f88\u5927\u7684\u901a\u7528\u6027\uff0c\u8bfb\u8005\u914c\u60c5\u53ef\u7ed5\u884c\u3002<\/p>\n

\u6807\u9898\u4e0d\u80fd\u5b8c\u5168\u8868\u8fbe\u672c\u610f\uff0c\u786e\u5207\u7684\u60c5\u666f\u9700\u8981\u5c55\u5f00\u8bf4\u3002\u5047\u8bbe\u6709\u4e09\u7ea7\u5206\u7c7b\uff0c\u5173\u4e8e\u5206\u7c7b\u8fd9\u6837\u8bbe\u8ba1\uff1a<\/p>\n

public class Category\r\n{\r\n    public int Id { get; set; }\r\n    public string Name { get; set; }\r\n    public int ParentId { get; set; }\r\n}<\/pre>\n

\u7136\u540e\u4ea7\u54c1\u53ef\u4ee5\u5c5e\u4e8e\u591a\u4e2a\u5206\u7c7b\uff0c\u4ee5\u4e0b\u7684Categories\u5c5e\u6027\u503c\u662f\u4ee5\u82f1\u6587\u9017\u53f7\u9694\u5f00\u3001\u7531\u5206\u7c7b\u7f16\u53f7\u62fc\u63a5\u800c\u6210\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n

public class Product\r\n{\r\n    public int Id { get; set; }\r\n    public string Name { get; set; }\r\n    public string Categories { get; set; }\r\n}<\/pre>\n

\u7531\u4e8e\u79cd\u79cd\u539f\u56e0\uff0cCategories\u5c5e\u6027\u503c\u53ea\u662f\u5b58\u50a8\u4e86\u7531\u7b2c\u4e09\u7ea7\u5206\u7c7b\u7f16\u53f7\u62fc\u63a5\u800c\u6210\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n

\u5728\u524d\u7aef\uff0c\u9700\u8981\u628a\u5206\u7c7b\u4f5c\u4e3a\u67e5\u8be2\u6761\u4ef6\u6765\u67e5\u8be2\u4ea7\u54c1\uff0c\u53ef\u80fd\u53ea\u9009\u62e9\u4e00\u7ea7\u5206\u7c7b\uff0c\u628a\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32(\u6bd4\u5982\"1\")\u53d1\u9001\u7ed9\u670d\u52a1\u7aef;\u53ef\u80fd\u540c\u65f6\u9009\u62e9\u4e00\u7ea7\u548c\u4e8c\u7ea7\u5206\u7c7b\uff0c\u4e5f\u628a\u4e00\u4e2a\u6570\u5b57\u5b57\u7b26\u4e32(\u6bd4\u5982\"1,2\")\u53d1\u9001\u7ed9\u670d\u52a1\u7aef\uff1b\u5f53\u7136\uff0c\u4e5f\u6709\u53ef\u80fd\u540c\u65f6\u9009\u62e9\u4e00\u7ea7\u3001\u4e8c\u7ea7\u548c\u4e09\u7ea7\u5206\u7c7b\u4f5c\u4e3a\u67e5\u8be2\u6761\u4ef6(\u6bd4\u5982\"1,2,3\")\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5982\u679c\u8bf8\u5982\"1\"\u6216\"1,2\"\u6216\"1,2,3\"\u8fd9\u6837\u7684\u67e5\u8be2\u6761\u4ef6\u8f6c\u6362\u6210\u6570\u7ec4\u540e\uff0c\u5982\u679c\u6570\u7ec4\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u88ab\u5305\u542b\u5728Product\u7684Categories\u5c5e\u6027\u503c\u8f6c\u6362\u6210\u7684\u6570\u7ec4\u4e2d\uff0c\u90a3\u8fd9\u4e2a\u4ea7\u54c1\u5c31\u7b26\u5408\u641c\u7d22\u6761\u4ef6\u3002<\/p>\n

\u7b80\u5355\u6765\u8bf4\uff0c\u662f\u8fd9\u6837\uff1a\u5047\u8bbe\u641c\u7d22\u6761\u4ef6\u662f\"1,2\"\uff0cProduct\u7684Categories\u5c5e\u6027\u503c\u4e3a\"1,3,2,5\"\uff0c\u6211\u4eec\u4e0d\u662f\u5224\u65ad\"1,2\"\u8fd9\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u5728\"1,3,2,5\"\u5b57\u7b26\u4e32\u4e2d\uff0c\u800c\u662f\u628a\"1,2\"\u5148split\u6210\u6570\u7ec4\uff0c\u53eb\u505aarray1, \u628a\"1,3,2,5\"\u4e5fsplit\u6210\u6570\u7ec4\uff0c\u53eb\u505aarray2\uff0c\u6700\u540e\u5224\u65adarray1\u7684\u6bcf\u4e2a\u5143\u7d20\u662f\u5426\u90fd\u88ab\u5305\u542b\u5728array2\u4e2d\u3002<\/p>\n

\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\u9700\u8981\u89e3\u51b3\uff1a\u5f53\u524d\u7684Product\u7684Categories\u5c5e\u6027\u503c\u53ea\u5b58\u50a8\u4e86\u6240\u6709\u7b2c\u4e09\u7ea7\u5206\u7c7b\u7f16\u53f7\u62fc\u63a5\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u524d\u7aef\u8f93\u5165\u7684\u641c\u7d22\u6761\u4ef6\u53ef\u80fd\u4f1a\u5305\u542b\u4e00\u7ea7\u5206\u7c7b\u6216\u4e8c\u7ea7\u5206\u7c7b\u7b49\uff0c\u6240\u4ee5\uff0c\u6211\u4eec\u9700\u8981\u628aProduct\u8f6c\u6362\u4e00\u4e0b\uff0c\u5e0c\u671b\u6709\u4e00\u4e2a\u7c7b\u7684\u67d0\u4e2a\u5c5e\u6027\u503c\u80fd\u5b58\u50a8\u7531\u4e00\u7ea7\u3001\u4e8c\u7ea7\u3001\u4e09\u7ea7\u5206\u7c7b\u62fc\u63a5\u800c\u6210\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n

public class ProductWithThreeCate\r\n{\r\n    public int Id { get; set; }\r\n    public string Name { get; set; }\r\n    public string AllCategoreis { get; set; }\r\n}<\/pre>\n

\u4ee5\u4e0a\uff0c AllCategoreis\u5c5e\u6027\u503c\u5c31\u7528\u6765\u5b58\u50a8\u7531\u4e00\u7ea7\u3001\u4e8c\u7ea7\u3001\u4e09\u7ea7\u5206\u7c7b\u62fc\u63a5\u800c\u6210\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n

\u6709\u4e00\u4e2a\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u5206\u7c7b\uff1a<\/p>\n

static List GetCategories()\r\n{\r\n    return new List<\/category>()\r\n    {\r\n        new Category(){Id = 1, Name = \"\u6839\", ParentId = -1},\r\n        new Category(){Id = 2, Name = \"\u4e00\u7ea7\u5206\u7c7b1\",ParentId = 1},\r\n        new Category(){Id = 3, Name = \"\u4e00\u7ea7\u5206\u7c7b2\", ParentId = 1},\r\n        new Category(){Id = 4, Name = \"\u4e8c\u7ea7\u5206\u7c7b11\",ParentId = 2},\r\n        new Category(){Id = 5, Name = \"\u4e8c\u7ea7\u5206\u7c7b12\",ParentId = 2},\r\n        new Category(){Id = 6, Name = \"\u4e8c\u7ea7\u5206\u7c7b21\",ParentId = 3},\r\n        new Category(){Id = 7, Name = \"\u4e8c\u7ea7\u5206\u7c7b22\",ParentId = 3},\r\n        new Category(){Id = 8, Name = \"\u4e09\u7ea7\u5206\u7c7b111\",ParentId = 4},\r\n        new Category(){Id = 9, Name = \"\u4e09\u7ea7\u5206\u7c7b112\",ParentId = 4},\r\n        new Category(){Id = 10, Name = \"\u4e09\u7ea7\u5206\u7c7b121\",ParentId = 5},\r\n        new Category(){Id = 11, Name = \"\u4e09\u7ea7\u5206\u7c7b122\",ParentId = 5},\r\n        new Category(){Id = 12, Name = \"\u4e09\u7ea7\u5206\u7c7b211\",ParentId = 6},\r\n        new Category(){Id = 13, Name = \"\u4e09\u7ea7\u5206\u7c7b212\",ParentId = 6},\r\n        new Category(){Id = 14, Name = \"\u4e09\u7ea7\u5206\u7c7b221\",ParentId = 7}\r\n    };\r\n}<\/category><\/pre>\n

\u6709\u4e00\u4e2a\u65b9\u6cd5\u83b7\u53d6\u6240\u6709\u4ea7\u54c1\uff1a<\/p>\n

static List GetProducts()\r\n{\r\n    return new List<\/product>()\r\n    {\r\n        new Product(){Id = 1, Name = \"\u4ea7\u54c11\",Categories = \"10,12\"},\r\n        new Product(){Id = 2, Name = \"\u4ea7\u54c12\", Categories = \"12,13\"},\r\n        new Product(){Id = 3, Name = \"\u4ea7\u54c13\",Categories = \"10,11,12\"},\r\n        new Product(){Id = 4, Name = \"\u4ea7\u54c14\",Categories = \"13,14\"},\r\n        new Product(){Id = 5, Name = \"\u4ea7\u54c15\",Categories = \"11,13,14\"}\r\n    };\r\n}<\/product><\/pre>\n

\u63a5\u4e0b\u6765\u7684\u65b9\u6cd5\u662f\u6839\u636e\u641c\u7d22\u6761\u4ef6(\u6bd4\u5982\u662f\"1,2\")\u6765\u67e5\u627e\u6ee1\u8db3\u6761\u4ef6\u7684ProductWithThreeCate\u96c6\u5408\uff0c\u5982\u4e0b\uff1a<\/p>\n

\/\/\/ < summary>\r\n\/\/\/ \u83b7\u53d6\u6ee1\u8db3\u67d0\u4e9b\u6761\u4ef6\u7684\u96c6\u5408\r\n\/\/\/ < \/summary>\r\n\/\/\/ < param name=\"query\">\u4ee5\u82f1\u6587\u9017\u53f7\u9694\u5f00\u7684\u5b57\u7b26\u4e32\uff0c\u6bd4\u5982\uff1a2,5< \/param>\r\n\/\/\/ < returns>\r\nstatic List GetResultByQuery(string query)\r\n{\r\n    \/\/\u6700\u7ec8\u7ed3\u679c\r\n    List<\/productwiththreecate> result = new List<\/productwiththreecate>();\r\n    \/\/\u4e34\u65f6\u7ed3\u679c \u6b64\u65f6ProductWithThreeCat\u7684\u5c5e\u6027AllCategoreis\u5305\u542b\u6240\u6709\u4e00\u7ea7\u3001\u4e8c\u7ea7\u3001\u4e09\u7ea7\u5206\u7c7bID\u62fc\u63a5\u6210\u7684\u5b57\u7b26\u4e32\r\n    List<\/productwiththreecate> tempResult = new List<\/productwiththreecate>();\r\n    \/\/\u83b7\u53d6\u6240\u6709\u7684\u4ea7\u54c1\r\n    List allProducts = GetProducts();\r\n    \/\/\u904d\u5386\u8fd9\u4e9b\u4ea7\u54c1\r\n    foreach (var item in allProducts)\r\n    {\r\n        ProductWithThreeCate productWithThreeCate = new ProductWithThreeCate();\r\n        productWithThreeCate.Id = item.Id;\r\n        productWithThreeCate.Name = item.Name;\r\n        \/\/\u6240\u6709\u4e00\u7ea7\u3001\u4e8c\u7ea7\u3001\u4e09\u7ea7\u62fc\u63a5\u6210\u4ee5\u82f1\u6587\u9017\u53f7\u9694\u5f00\u7684\u5b57\u7b26\u4e32\r\n        string temp = string.Empty;\r\n        \/\/\u5f53\u524d\u4ea7\u54c1\u53ea\u5305\u542b\u4e09\u7ea7\u62fc\u63a5\u6210\u7684\u3001\u4e5f\u662f\u4ee5\u82f1\u6587\u9694\u5f00\u7684\u5b57\u7b26\u4e32\uff0csplit\u6210\u6570\u7ec4\r\n        string[] theThirdCates = item.Categories.Split(',');\r\n        \/\/\u904d\u5386\u8fd9\u4e9b\u4e09\u7ea7\u6570\u7ec4\r\n        foreach (string i in theThirdCates)\r\n        {\r\n            \/\/\u4e09\u7ea7\u7c7b\u522b\u8f6c\u6362\u6210\u6574\u578b\r\n            int theThirdInt = int.Parse(i);\r\n            \/\/\u83b7\u53d6\u4e09\u7ea7\u7c7b\u522b\r\n            Category theThirdCate = GetCategories().Where(c => c.Id == theThirdInt).FirstOrDefault();\r\n            \/\/\u83b7\u53d6\u4e8c\u7ea7\u7c7b\u522b\r\n            Category theSecondCate = GetCategories().Where(c => c.Id == theThirdCate.ParentId).FirstOrDefault();\r\n            \/\/\u83b7\u53d6\u4e00\u7ea7\u7c7b\u522b\r\n            Category theFirstCate = GetCategories().Where(c => c.Id == theSecondCate.ParentId).FirstOrDefault();\r\n            temp += i + \",\" + theSecondCate.Id.ToString() + \",\" + theFirstCate.Id.ToString() + \",\";\r\n        }\r\n        \/\/\u53bb\u6389\u6700\u540e\u4e00\u4e2a\u82f1\u6587\u9017\u53f7\r\n        temp = temp.Substring(0, temp.Length - 1);\r\n        \/\/\u8f6c\u6362\u6210\u96c6\u5408\uff0c\u53bb\u9664\u91cd\u590d\u9879\uff0c\u6bd4\u5982\u4e0d\u540c\u7684\u4e09\u7ea7\u53ef\u80fd\u6709\u76f8\u540c\u7684\u4e00\u7ea7\u6216\u4e8c\u7ea7\u7236\u7c7b\r\n        IEnumerable tempArray = temp.Split(',').AsEnumerable().Distinct();\r\n        \/\/\u6240\u6709\u4e00\u7ea7\u3001\u4e8c\u7ea7\u3001\u4e09\u7ea7\u62fc\u63a5\u6210\u4ee5\u82f1\u6587\u9017\u53f7\u9694\u5f00\u7684\u5b57\u7b26\u4e32\uff0c\u4f46\u5df2\u7ecf\u53bb\u9664\u4e86\u91cd\u590d\u7684\u4e00\u7ea7\u548c\u4e8c\u7ea7\r\n        string tempagain = string.Empty;\r\n        \/\/\u518d\u6b21\u904d\u5386\u96c6\u5408\u62fc\u63a5\u6210\u5b57\u7b26\u4e32\r\n        foreach (var s in tempArray)\r\n        {\r\n            tempagain += s + \",\";\r\n        }\r\n        productWithThreeCate.AllCategoreis = tempagain.Substring(0, tempagain.Length - 1);\r\n        tempResult.Add(productWithThreeCate);\r\n    }\r\n    \/\/\u904d\u5386\u4e34\u65f6\u7ed3\u679c\r\n    foreach (var item in tempResult)\r\n    {\r\n        \/\/\u628a\u5f53\u524d\u5305\u542b\u4e00\u7ea7\u3001\u4e8c\u7ea7\u3001\u4e09\u7ea7\u7684\uff0c\u4ee5\u82f1\u6587\u9017\u53f7\u9694\u5f00\u7684\u5b57\u7b26\u4e32split\u6210\u6570\u7ec4\r\n        string[] itemArray = item.AllCategoreis.Split(',');\r\n        \/\/\u628a\u5f53\u524d\u67e5\u8be2\u5b57\u7b26\u4e32split\u6210\u6570\u7ec4\r\n        string[] queryArray = query.Split(',');\r\n        \/\/\u5982\u679cqueryArray\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\u90fd\u88ab\u5305\u542b\u5728itemArray\u4e2d\uff0c\u90a3\u5c31\u4fdd\u5b58\u8d77\u6765\r\n        if (queryArray.All(x => itemArray.Contains(x)) == true)\r\n        {\r\n            result.Add(item);\r\n        }\r\n    }\r\n    return result;\r\n}<\/string><\/product><\/productwiththreecate><\/pre>\n

\u5ba2\u6237\u7aef\u7684\u8c03\u7528\u5982\u4e0b\uff1a<\/p>\n

List result = GetResultByQuery(\"2,5\");\r\n\/\/\u904d\u5386\u6700\u7ec8\u7684\u7ed3\u679c\r\nforeach (var item in result)\r\n{\r\n    Console.WriteLine(item.Name+ \"  \" + item.AllCategoreis);\r\n}\r\nConsole.ReadKey();<\/productwiththreecate><\/pre>\n

\u4ee5\u4e0a\u5c31\u662f\u8fd9\u7bc7\u6587\u7ae0\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u5e0c\u671b\u672c\u6587\u7684\u5185\u5bb9\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u8005\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u5b66\u4e60\u4ef7\u503c<\/p>\n","protected":false},"excerpt":{"rendered":"

\u672c\u7bc7\u662f\u9488\u5bf9\u6211\u5728\u505a\u9879\u76ee\u8fc7\u7a0b\u4e2d\u9047\u5230\u7684\u7279\u5b9a\u9700\u6c42\u800c\u505a\u7684\u4e00\u4e2aDemo, \u6ca1\u6709\u5f88\u5927\u7684\u901a\u7528\u6027\uff0c\u8bfb\u8005\u914c\u60c5\u53ef\u7ed5\u884c\u3002 \u6807\u9898\u4e0d\u80fd\u5b8c\u5168 […]<\/p>\n","protected":false},"author":668,"featured_media":147183,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-256170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=256170"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256170\/revisions"}],"predecessor-version":[{"id":256174,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/256170\/revisions\/256174"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/147183"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=256170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=256170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=256170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}