{"id":1209,"date":"2025-01-25T22:59:55","date_gmt":"2025-01-25T14:59:55","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1209"},"modified":"2025-01-25T22:59:56","modified_gmt":"2025-01-25T14:59:56","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e4%b8%ad%e9%ab%98%e6%95%88%e5%90%88%e5%b9%b6%e5%a4%9a%e4%b8%aa%e5%ad%97%e5%85%b8%e5%b9%b6%e5%a4%84%e7%90%86%e9%94%ae%e5%86%b2%e7%aa%81","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8-c-%e4%b8%ad%e9%ab%98%e6%95%88%e5%90%88%e5%b9%b6%e5%a4%9a%e4%b8%aa%e5%ad%97%e5%85%b8%e5%b9%b6%e5%a4%84%e7%90%86%e9%94%ae%e5%86%b2%e7%aa%81\/","title":{"rendered":"\u5982\u4f55\u5728 C# \u4e2d\u9ad8\u6548\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u5e76\u5904\u7406\u952e\u51b2\u7a81"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 C# \u4e2d\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u5e76\u5904\u7406\u952e\u51b2\u7a81\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u9ad8\u6548\u7684\u5b57\u5178\u5408\u5e76\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 LINQ\u3001<code>ToString<\/code>\u00a0\u683c\u5f0f\u5316\u548c\u624b\u52a8\u51b2\u7a81\u5904\u7406\u7b49\u7b56\u7565\u3002\u65e0\u8bba\u662f\u4fdd\u6301\u6700\u540e\u4e00\u4e2a\u503c\u8fd8\u662f\u81ea\u5b9a\u4e49\u5408\u5e76\u89c4\u5219\uff0c\u672c\u6587\u4e3a\u4f60\u63d0\u4f9b\u4e86\u5e94\u5bf9\u4e0d\u540c\u5b57\u5178\u64cd\u4f5c\u573a\u666f\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5e2e\u52a9\u4f60\u8f7b\u677e\u5904\u7406\u5b57\u5178\u4e2d\u7684\u91cd\u590d\u952e\u95ee\u9898\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528 <code>Union<\/code> \u64cd\u4f5c\u7b26 (LINQ)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">LINQ \u63d0\u4f9b\u4e86\u4e00\u4e2a <code>Union<\/code> \u64cd\u4f5c\u7b26\uff0c\u80fd\u591f\u5408\u5e76\u4e24\u4e2a\u8bcd\u5178\uff0c\u5e76\u786e\u4fdd\u4e0d\u4f1a\u6709\u91cd\u590d\u7684\u952e\u3002\u5982\u679c\u4f60\u60f3\u5408\u5e76\u591a\u4e2a\u8bcd\u5178\uff0c\u5e76\u4e14\u5e0c\u671b\u4fdd\u7559\u6700\u540e\u4e00\u4e2a\u51fa\u73b0\u7684\u952e\u503c\u5bf9\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>GroupBy<\/code> \u6765\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.Linq;\n\nclass Program\n{\n    static void Main()\n    {\n        var dict1 = new Dictionary&lt;string, int&gt; { { \"a\", 1 }, { \"b\", 2 } };\n        var dict2 = new Dictionary&lt;string, int&gt; { { \"b\", 3 }, { \"c\", 4 } };\n        var dict3 = new Dictionary&lt;string, int&gt; { { \"d\", 5 } };\n\n        var combined = dict1\n            .Concat(dict2)\n            .Concat(dict3)\n            .GroupBy(pair =&gt; pair.Key)\n            .ToDictionary(g =&gt; g.Key, g =&gt; g.Last().Value);\n\n        foreach (var kvp in combined)\n        {\n            Console.WriteLine($\"{kvp.Key}: {kvp.Value}\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Concat()<\/code> \u65b9\u6cd5\u5c06\u591a\u4e2a\u8bcd\u5178\u5408\u5e76\u6210\u4e00\u4e2a\u5e8f\u5217\u3002<\/li>\n\n\n\n<li><code>GroupBy()<\/code> \u7528\u4e8e\u6309\u952e\u5206\u7ec4\uff0c\u4ee5\u786e\u4fdd\u6bcf\u4e2a\u952e\u53ea\u6709\u4e00\u4e2a\u503c\u3002<\/li>\n\n\n\n<li><code>Last()<\/code> \u7528\u4e8e\u4fdd\u7559\u6700\u540e\u51fa\u73b0\u7684\u952e\u503c\u5bf9\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7b80\u6d01\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u6027\u80fd\u8003\u8651\uff0c\u5c24\u5176\u662f\u5904\u7406\u975e\u5e38\u5927\u7684\u8bcd\u5178\u65f6\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>AddRange()<\/code> \u65b9\u6cd5 (\u5bf9\u4e8e <code>SortedDictionary<\/code> \u6216 <code>ListDictionary<\/code>)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4f7f\u7528\u7684\u662f <code>SortedDictionary<\/code> \u6216 <code>ListDictionary<\/code>\uff0c\u53ef\u4ee5\u5229\u7528 <code>AddRange()<\/code> \u65b9\u6cd5\u6765\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u3002\u6ce8\u610f <code>AddRange()<\/code> \u65b9\u6cd5\u9002\u7528\u4e8e\u7279\u5b9a\u7c7b\u578b\u7684\u5b57\u5178\uff0c\u56e0\u6b64\u4e0d\u9002\u7528\u4e8e <code>Dictionary&lt;TKey, TValue&gt;<\/code>\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\nclass Program\n{\n    static void Main()\n    {\n        var dict1 = new Dictionary&lt;string, int&gt; { { \"a\", 1 }, { \"b\", 2 } };\n        var dict2 = new Dictionary&lt;string, int&gt; { { \"b\", 3 }, { \"c\", 4 } };\n\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5b57\u5178\u6765\u5b58\u50a8\u5408\u5e76\u7684\u7ed3\u679c\n        var merged = new Dictionary&lt;string, int&gt;(dict1);\n\n        foreach (var kvp in dict2)\n        {\n            merged&#91;kvp.Key] = kvp.Value;  \/\/ \u5982\u679c\u952e\u5df2\u5b58\u5728\uff0c\u66f4\u65b0\u5176\u503c\n        }\n\n        foreach (var kvp in merged)\n        {\n            Console.WriteLine($\"{kvp.Key}: {kvp.Value}\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u79cd\u65b9\u6cd5\u9010\u4e2a\u904d\u5386\u5b57\u5178\uff0c\u5e76\u5c06 <code>dict2<\/code> \u4e2d\u7684\u952e\u503c\u5bf9\u6dfb\u52a0\u5230 <code>merged<\/code> \u4e2d\uff0c\u5982\u679c\u6709\u91cd\u590d\u7684\u952e\uff0c\u4f1a\u66f4\u65b0\u4e3a\u6700\u65b0\u7684\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u4f7f\u7528 <code>ToDictionary<\/code> \u65b9\u6cd5 (LINQ)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u8981\u5408\u5e76\u591a\u4e2a\u8bcd\u5178\u5e76\u5e0c\u671b\u5728\u5408\u5e76\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u7279\u5b9a\u7684\u903b\u8f91\u5904\u7406\uff08\u5982\u81ea\u5b9a\u4e49\u5408\u5e76\u89c4\u5219\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>ToDictionary<\/code> \u548c <code>SelectMany<\/code> \u6765\u7075\u6d3b\u5904\u7406\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\nusing System.Linq;\n\nclass Program\n{\n    static void Main()\n    {\n        var dict1 = new Dictionary&lt;string, int&gt; { { \"a\", 1 }, { \"b\", 2 } };\n        var dict2 = new Dictionary&lt;string, int&gt; { { \"b\", 3 }, { \"c\", 4 } };\n        var dict3 = new Dictionary&lt;string, int&gt; { { \"a\", 5 }, { \"d\", 6 } };\n\n        var combined = new&#91;] { dict1, dict2, dict3 }\n            .SelectMany(d =&gt; d)\n            .GroupBy(kvp =&gt; kvp.Key)\n            .ToDictionary(g =&gt; g.Key, g =&gt; g.Last().Value);\n\n        foreach (var kvp in combined)\n        {\n            Console.WriteLine($\"{kvp.Key}: {kvp.Value}\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SelectMany<\/code> \u4f1a\u5c06\u591a\u4e2a\u5b57\u5178\u5c55\u5e73\u6210\u4e00\u4e2a\u5e8f\u5217\u3002<\/li>\n\n\n\n<li>\u7136\u540e\u4f7f\u7528 <code>GroupBy<\/code> \u548c <code>Last()<\/code> \u6765\u53bb\u9664\u91cd\u590d\u952e\uff0c\u53ea\u4fdd\u7559\u6bcf\u4e2a\u952e\u7684\u6700\u540e\u4e00\u4e2a\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u624b\u52a8\u5408\u5e76\u5e76\u5904\u7406\u51b2\u7a81<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u60f3\u8981\u66f4\u52a0\u81ea\u5b9a\u4e49\u7684\u5408\u5e76\u65b9\u5f0f\uff08\u6bd4\u5982\u6309\u67d0\u79cd\u89c4\u5219\u5408\u5e76\u51b2\u7a81\u7684\u952e\u503c\uff09\uff0c\u53ef\u4ee5\u624b\u52a8\u904d\u5386\u8bcd\u5178\u5e76\u5408\u5e76\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Collections.Generic;\n\nclass Program\n{\n    static void Main()\n    {\n        var dict1 = new Dictionary&lt;string, int&gt; { { \"a\", 1 }, { \"b\", 2 } };\n        var dict2 = new Dictionary&lt;string, int&gt; { { \"b\", 3 }, { \"c\", 4 } };\n\n        var merged = new Dictionary&lt;string, int&gt;(dict1);\n\n        foreach (var kvp in dict2)\n        {\n            if (merged.ContainsKey(kvp.Key))\n            {\n                \/\/ \u81ea\u5b9a\u4e49\u5408\u5e76\u89c4\u5219\uff0c\u8fd9\u91cc\u7b80\u5355\u5730\u5c06\u4e24\u4e2a\u503c\u76f8\u52a0\n                merged&#91;kvp.Key] += kvp.Value;\n            }\n            else\n            {\n                merged.Add(kvp.Key, kvp.Value);\n            }\n        }\n\n        foreach (var kvp in merged)\n        {\n            Console.WriteLine($\"{kvp.Key}: {kvp.Value}\");\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u89e3\u91ca\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u901a\u8fc7\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728\u6765\u51b3\u5b9a\u5982\u4f55\u5408\u5e76\u51b2\u7a81\u7684\u503c\uff0c\u8fd9\u6837\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u5b9a\u4e49\u5408\u5e76\u89c4\u5219\uff08\u5982\u52a0\u6cd5\u3001\u9009\u62e9\u8f83\u5927\u503c\u7b49\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u6700\u4f73\u5b9e\u8df5\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6027\u80fd\u8003\u91cf<\/strong>\uff1a\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\uff0c\u907f\u514d\u8fc7\u591a\u7684 <code>GroupBy()<\/code> \u6216 <code>Concat()<\/code> \u64cd\u4f5c\u3002\u53ef\u4ee5\u901a\u8fc7\u76f4\u63a5\u904d\u5386\u5b57\u5178\u6765\u4f18\u5316\u6027\u80fd\u3002<\/li>\n\n\n\n<li><strong>\u5408\u5e76\u7b56\u7565<\/strong>\uff1a\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u662f\u5426\u9700\u8981\u5904\u7406\u952e\u51b2\u7a81\u3002\u5982\u679c\u4f60\u53ea\u662f\u5e0c\u671b\u4fdd\u7559\u6700\u65b0\u7684\u503c\uff0c<code>GroupBy()<\/code> \u6216 <code>AddRange()<\/code> \u90fd\u662f\u5f88\u597d\u7684\u9009\u62e9\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 C# \u4e2d\u5408\u5e76\u591a\u4e2a\u5b57\u5178\u5e76\u5904\u7406\u952e\u51b2\u7a81\u662f\u4e00\u9879\u5e38\u89c1\u7684\u4efb\u52a1\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u9ad8\u6548\u7684\u5b57\u5178\u5408\u5e76\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528 LINQ\u3001T [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[],"class_list":["post-1209","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1209"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1209\/revisions"}],"predecessor-version":[{"id":1210,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1209\/revisions\/1210"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}