{"id":1235,"date":"2025-01-25T23:38:08","date_gmt":"2025-01-25T15:38:08","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1235"},"modified":"2025-01-25T23:38:09","modified_gmt":"2025-01-25T15:38:09","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-linq-to-entities-%e4%b8%ad%e5%b0%86%e6%95%b4%e6%95%b0%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%9a%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97","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-%e4%b8%ad%e5%b0%86%e6%95%b4%e6%95%b0%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%9a%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97\/","title":{"rendered":"\u5982\u4f55\u5728 LINQ to Entities \u4e2d\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1a\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5728 LINQ to Entities \u67e5\u8be2\u4e2d\uff0c\u5c06\u6574\u6570 (<code>int<\/code>) \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32 (<code>string<\/code>) \u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u64cd\u4f5c\uff0c\u7136\u800c\uff0c\u7531\u4e8e LINQ to Entities \u67e5\u8be2\u4f1a\u88ab\u8f6c\u6362\u4e3a SQL\uff0c\u56e0\u6b64\u4e0d\u80fd\u76f4\u63a5\u4f7f\u7528 <code>ToString()<\/code> \u65b9\u6cd5\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u60a8\u9700\u8981\u4f7f\u7528 <code>SqlFunctions.StringConvert<\/code> \u65b9\u6cd5\uff0c\u8fd9\u662f Entity Framework \u63d0\u4f9b\u7684\u4e00\u4e2a\u7279\u5b9a\u51fd\u6570\uff0c\u53ef\u4ee5\u5728\u67e5\u8be2\u6267\u884c\u65f6\u5c06\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 <code>SqlFunctions.StringConvert<\/code> \u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SqlFunctions.StringConvert<\/code> \u662f\u4e00\u4e2a\u53ef\u4ee5\u5c06\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002\u5b83\u5728\u6267\u884c\u67e5\u8be2\u65f6\u4f1a\u8f6c\u6362\u4e3a\u9002\u5f53\u7684 SQL \u51fd\u6570\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 \u547d\u540d\u7a7a\u95f4\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)  \/\/ \u8f6c\u6362 int \u4e3a string\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><strong><code>SqlFunctions.StringConvert<\/code><\/strong>\uff1a\u7528\u4e8e\u5c06\u6570\u503c\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>int<\/code> \u503c\u9700\u8981\u5148\u8f6c\u6362\u4e3a <code>double<\/code> \u7c7b\u578b\uff0c\u56e0\u4e3a <code>StringConvert<\/code> \u65b9\u6cd5\u7684\u53c2\u6570\u7c7b\u578b\u8981\u6c42\u662f\u6570\u503c\u7c7b\u578b\u3002<\/li>\n\n\n\n<li><strong><code>YourIntValue<\/code><\/strong>\uff1a\u662f\u60a8\u8981\u8f6c\u6362\u7684 <code>int<\/code> \u7c7b\u578b\u5b57\u6bb5\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e3a\u4ec0\u4e48\u8981\u8f6c\u6362\u4e3a <code>double<\/code>\uff1f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SqlFunctions.StringConvert<\/code> \u53ea\u63a5\u53d7\u6570\u503c\u7c7b\u578b\u4f5c\u4e3a\u53c2\u6570\uff08\u5982 <code>decimal<\/code>, <code>double<\/code>\uff09\uff0c\u56e0\u6b64\u9700\u8981\u663e\u5f0f\u5730\u5c06 <code>int<\/code> \u8f6c\u6362\u4e3a <code>double<\/code>\uff0c\u8fd9\u662f SQL \u4e2d\u5904\u7406\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u4e00\u79cd\u65b9\u5f0f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u6027\u80fd<\/strong>\uff1a<code>SqlFunctions.StringConvert<\/code> \u4f1a\u5728\u6570\u636e\u5e93\u4e2d\u8fdb\u884c\u5904\u7406\uff0c\u56e0\u6b64\u5b83\u9002\u7528\u4e8e\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u7cbe\u5ea6\u95ee\u9898<\/strong>\uff1a\u8f6c\u6362\u4e3a <code>double<\/code> \u540e\uff0c\u6570\u5b57\u53ef\u80fd\u4f1a\u51fa\u73b0\u7cbe\u5ea6\u635f\u5931\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u975e\u5e38\u5927\u7684\u6574\u6570\u3002\u5982\u679c\u7cbe\u5ea6\u5f88\u91cd\u8981\uff0c\u53ef\u4ee5\u8003\u8651\u5176\u4ed6\u65b9\u5f0f\u6765\u5904\u7406\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3\uff1a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u5728 LINQ to Entities \u67e5\u8be2\u4e2d\u5c06 <code>int<\/code> \u8f6c\u6362\u4e3a <code>string<\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>SqlFunctions.StringConvert<\/code> \u65b9\u6cd5\uff0c\u5e76\u786e\u4fdd\u5c06\u6574\u6570\u7c7b\u578b\u8f6c\u6362\u4e3a <code>double<\/code> \u7c7b\u578b\uff0c\u4ee5\u907f\u514d\u8fd0\u884c\u65f6\u9519\u8bef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 LINQ to Entities \u67e5\u8be2\u4e2d\uff0c\u5c06\u6574\u6570 (int) \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32 (string) \u662f\u4e00\u4e2a\u5e38\u89c1\u7684 [&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-1235","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1235","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=1235"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1235\/revisions"}],"predecessor-version":[{"id":1236,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1235\/revisions\/1236"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}