{"id":1233,"date":"2025-01-25T23:36:07","date_gmt":"2025-01-25T15:36:07","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1233"},"modified":"2025-01-25T23:36:08","modified_gmt":"2025-01-25T15:36:08","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-linq-to-entities-%e6%9f%a5%e8%af%a2%e4%b8%ad%e5%b0%86-int-%e8%bd%ac%e6%8d%a2%e4%b8%ba-string%ef%bc%9a%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/01\/25\/%e5%a6%82%e4%bd%95%e5%9c%a8-linq-to-entities-%e6%9f%a5%e8%af%a2%e4%b8%ad%e5%b0%86-int-%e8%bd%ac%e6%8d%a2%e4%b8%ba-string%ef%bc%9a%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5\/","title":{"rendered":"\u5982\u4f55\u5728 LINQ to Entities \u67e5\u8be2\u4e2d\u5c06 Int \u8f6c\u6362\u4e3a String\uff1a\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 LINQ to Entities \u67e5\u8be2\u4e2d\uff0c\u5c06 <code>int<\/code> \u8f6c\u6362\u4e3a <code>string<\/code> \u5e76\u4e0d\u50cf\u5728 LINQ to Objects \u4e2d\u90a3\u6837\u76f4\u63a5\u4f7f\u7528 <code>ToString()<\/code> \u65b9\u6cd5\u3002\u8fd9\u662f\u56e0\u4e3a <code>ToString()<\/code> \u5728\u6267\u884c\u5230\u6570\u636e\u5e93\u65f6\u5e76\u4e0d\u53ef\u7528\uff0c\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4f7f\u7528 LINQ to Entities \u652f\u6301\u7684\u51fd\u6570\u6765\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>\u4f7f\u7528 <code>SqlFunctions.StringConvert<\/code> \u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 LINQ to Entities \u67e5\u8be2\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>SqlFunctions.StringConvert<\/code> \u6765\u5c06 <code>int<\/code> \u8f6c\u6362\u4e3a <code>string<\/code>\uff0c\u8fd9\u662f Entity Framework \u63d0\u4f9b\u7684\u4e00\u4e2a\u5185\u7f6e\u51fd\u6570\uff0c\u4e13\u95e8\u7528\u4e8e\u5904\u7406 SQL \u6570\u636e\u5e93\u4e2d\u7684\u8f6c\u6362\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.Linq;\nusing System.Data.Entity.SqlServer; \/\/ \u5f15\u5165 SqlFunctions\n\nclass Program\n{\n    static void Main()\n    {\n        using (var context = new YourDbContext())\n        {\n            var result = from entity in context.YourEntities\n                         select new\n                         {\n                             ConvertedValue = SqlFunctions.StringConvert((double)entity.YourIntValue)\n                         };\n\n            foreach (var item in result)\n            {\n                Console.WriteLine(item.ConvertedValue);  \/\/ \u8f93\u51fa\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u503c\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u8bf4\u660e\uff1a<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>SqlFunctions.StringConvert<\/code> \u53ea\u80fd\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a <code>string<\/code>\u3002<\/li>\n\n\n\n<li>\u9700\u8981\u663e\u5f0f\u5730\u5c06 <code>int<\/code> \u8f6c\u6362\u4e3a <code>double<\/code>\uff0c\u56e0\u4e3a <code>StringConvert<\/code> \u53ea\u63a5\u53d7\u6570\u503c\u7c7b\u578b\uff08\u5982 <code>double<\/code>\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>\u4f7f\u7528 <code>Select<\/code> \u548c <code>ToString<\/code> \u65b9\u6cd5\uff08\u4ec5\u9002\u7528\u4e8e LINQ to Objects\uff09<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u5df2\u7ecf\u5c06\u67e5\u8be2\u7ed3\u679c\u4ece\u6570\u636e\u5e93\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff08\u5373\u6267\u884c\u4e86 <code>ToList()<\/code> \u6216 <code>ToArray()<\/code>\uff09\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>ToString()<\/code> \u65b9\u6cd5\u6765\u8f6c\u6362\u4e3a <code>string<\/code>\u3002\u8fd9\u79cd\u65b9\u5f0f\u53ea\u9002\u7528\u4e8e LINQ to Objects\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.Linq;\n\nclass Program\n{\n    static void Main()\n    {\n        using (var context = new YourDbContext())\n        {\n            var result = context.YourEntities\n                                .Select(entity =&gt; new\n                                {\n                                    ConvertedValue = entity.YourIntValue.ToString()\n                                })\n                                .ToList(); \/\/ \u67e5\u8be2\u6267\u884c\u5230\u5185\u5b58\n\n            foreach (var item in result)\n            {\n                Console.WriteLine(item.ConvertedValue);  \/\/ \u8f93\u51fa\u8f6c\u6362\u540e\u7684\u5b57\u7b26\u4e32\u503c\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>LINQ to Entities<\/strong>\uff1a\u4f7f\u7528 <code>SqlFunctions.StringConvert<\/code> \u65b9\u6cd5\u5c06 <code>int<\/code> \u8f6c\u6362\u4e3a <code>string<\/code>\u3002<\/li>\n\n\n\n<li><strong>LINQ to Objects<\/strong>\uff1a\u53ef\u4ee5\u4f7f\u7528 <code>.ToString()<\/code> \u65b9\u6cd5\u8fdb\u884c\u8f6c\u6362\uff0c\u4f46\u8fd9\u9700\u8981\u67e5\u8be2\u7ed3\u679c\u5df2\u7ecf\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 LINQ to Entities \u67e5\u8be2\u4e2d\uff0c\u5c06 int \u8f6c\u6362\u4e3a string \u5e76\u4e0d\u50cf\u5728 LINQ to Ob [&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-1233","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1233","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=1233"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1233\/revisions"}],"predecessor-version":[{"id":1234,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1233\/revisions\/1234"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}