{"id":1695,"date":"2011-01-21T09:45:09","date_gmt":"2011-01-21T00:45:09","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/1695"},"modified":"2011-01-23T08:29:04","modified_gmt":"2011-01-22T23:29:04","slug":"linq-to-entities-%e3%81%a7%e3%81%af-tostring-%e3%81%8c%e4%bd%bf%e3%81%88%e3%81%aa%e3%81%84","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/1695","title":{"rendered":"LINQ to Entities \u3067\u306f ToString \u304c\u4f7f\u3048\u306a\u3044"},"content":{"rendered":"<p>\n    Entity Data Model\u3068LINQ to SQL \u306f\u540c\u6642\u306b\u4f7f\u3048\u306a\u3044 | Moonmile Solutions Blog<br \/>\n    <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/1688\">http:\/\/www.moonmile.net\/blog\/archives\/1688<\/a>\n<\/p>\n<p>\n    \u524d\u56de\u306e\u7d9a\u304d\u3068\u3044\u3046\u3053\u3068\u3067\u3001Entity Data Model \u306e\u4f7f\u3044\u3069\u3053\u308d\u3001\u3068\u3044\u3046\u304b\u79c1\u304c\u9665\u3063\u305f\u843d\u3068\u3057\u7a74\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002\n<\/p>\n<p>\n    \u3055\u3066\u5b9f\u306f\u3001ASP.NET MVC \u306e\u9023\u8f09\u8a18\u4e8b\u3092\u66f8\u3044\u3066\u3044\u308b\u9014\u4e2d\u3067\u3001Html.DropDownList \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3044\u305f\u304b\u3063\u305f\u306e\u3067\u3059\u306d\u3002\u9023\u8f09\u8a18\u4e8b\u306e\u307b\u3046\u306f\u3001Entity Data Model \u3092\u4f7f\u3046\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u306e\u3067\u3001DropDownList \u306b\u9023\u7d50\u3059\u308b\u306a\u3089\u3070\u3001\u3084\u3063\u3071\u308a\u300c\u30c7\u30fc\u30bf\u30d0\u30a4\u30f3\u30c9\u300d\u3067\u3059\u306d\u3001\u3063\u3066\u5f62\u306b\u3057\u305f\u304b\u3063\u305f\u306e\u3067\u3059\u3002\n<\/p>\n<p>\n    \u4ee5\u524d\u306e ASP.NET \u30b3\u30f3\u30c8\u30ed\u30fc\u30eb\u3067\u3042\u308c\u3070\u3001\n<\/p>\n<p>\n    dropDownList.DataSource = items ;\n<\/p>\n<p>\n    \u307f\u305f\u3044\u306a\u5f62\u3067\u30c7\u30fc\u30bf\u30bd\u30fc\u30b9\u3092\u6307\u5b9a\u3059\u308b\u308f\u3051\u3067\u3059\u304c\u3001HTML \u30d8\u30eb\u30d1\u30fc\u30e1\u30bd\u30c3\u30c9\u306e\u5834\u5408\u306f\u3001\n<\/p>\n<p>\n    Html.DropDownList( &#8220;states&#8221; );\n<\/p>\n<p>\n    \u306e\u3088\u3046\u306b ViewData \u3092\u4f7f\u3063\u3066\u30d0\u30a4\u30f3\u30c9\u3055\u305b\u308b\u65b9\u6cd5\u3082\u3042\u308c\u3070\u3001\n<\/p>\n<p>\n    Html.DropDownList( &#8220;states&#8221;, Model.items );\n<\/p>\n<p>\n    \u306e\u3088\u3046\u306b\u30e2\u30c7\u30eb\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u8a2d\u5b9a\u3059\u308b\u65b9\u6cd5\u3082\u3042\u308a\u307e\u3059\u3002\n<\/p>\n<p>\n    \u305d\u3053\u3067\u90fd\u9053\u5e9c\u770c\u30b3\u30fc\u30c9\u3092\u30d0\u30a4\u30f3\u30c9\u3057\u3088\u3046\u304b\u306a\u3041\u3001\u3068\u601d\u3063\u3066\u3001\u90fd\u9053\u5e9c\u770c\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3002\n<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ncreate table TStates (\r\n\tid int, -- \u90fd\u9053\u5e9c\u770c\u30b3\u30fc\u30c9\r\n\tname varchar(20) -- \u90fd\u9053\u5e9c\u770c\u540d\r\n)\r\n<\/pre>\n<\/p>\n<p>\n    \u305d\u3057\u3066\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u4f5c\u6210\u3002\n<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npublic ActionResult StatesList()\r\n{\r\n    Models.mvcdbEntities ent = new Models.mvcdbEntities();\r\n\r\n    var items = from t in ent.TStates\r\n                select new SelectListItem\r\n                {\r\n                    Value = t.id.ToString(),\r\n                    Text = t.name\r\n                };\r\n    ViewData&#x5B;&quot;states&quot;] = items;\r\n\r\n    return View();\r\n}\r\n<\/pre>\n<\/p>\n<p>\n    DropDownList \u306f\u3001SelectListItem \u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3057\u304b\u53d7\u3051\u4ed8\u3051\u306a\u3044\u306e\u3067\u3001\u3042\u3089\u305f\u306b new \u3057\u3066\u8ffd\u52a0\u3057\u307e\u3059\u3002\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u691c\u7d22\u3067\u304d\u305f\u307b\u3046\u304c\u3044\u3044\u306e\u3067\u3001LINQ \u304c\u4fbf\u5229\u3067\u3059\u3088\u306d\u3002\u3063\u3066\u8a33\u3067\u3001LINQ \u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002<br \/>\n    SelectListItem \u306f\u3001Text \u306f\u8868\u793a\u6587\u5b57\u5217\u3001Value \u306f\u5024\u306a\u3093\u3067\u3059\u304c\u30d6\u30e9\u30a6\u30b6\u306a\u306e\u3067 ToString \u3067\u5909\u63db\u3057\u3066\u3044\u307e\u3059\uff08\u3053\u3053\u304c\u843d\u3068\u3057\u7a74\u3067\u3059\uff09\u3002\n<\/p>\n<p>\n    \u3055\u3066\u3001\u3053\u308c\u3092\u52d5\u304b\u3059\u3068\u3001\n<\/p>\n<p>\n    \u25bc\u753b\u50cf001\n<\/p>\n<p>\n    <a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2011\/01\/bw_uploads20110121_01.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"20110121_01.jpg\" border=\"0\" alt=\"20110121_01.jpg\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2011\/01\/bw_uploadstm_20110121_01.jpg\" width=\"401\" height=\"228\" \/><\/a>\n<\/p>\n<p>\n    \u3063\u3066\u306a\u30a8\u30e9\u30fc\u304c\u3067\u307e\u3059\u3002\n<\/p>\n<blockquote style=\"MARGIN-RIGHT: 0px\" dir=\"ltr\">\n<p>\n        System.NotSupportedException \u306f\u30e6\u30fc\u30b6\u30fc \u30b3\u30fc\u30c9\u306b\u3088\u3063\u3066\u30cf\u30f3\u30c9\u30eb\u3055\u308c\u307e\u305b\u3093\u3067\u3057\u305f\u3002<br \/>\n        &nbsp; Message=\u30e1\u30bd\u30c3\u30c9 &#8216;System.String ToString()&#8217; \u306f LINQ to Entities \u3067\u306f\u8a8d\u8b58\u3055\u308c\u306a\u3044\u305f\u3081\u3001\u30b9\u30c8\u30a2\u5f0f\u306b\u5909\u63db\u3067\u304d\u307e\u305b\u3093\u3002<br \/>\n        &nbsp; Source=System.Data.Entity\n    <\/p>\n<\/blockquote>\n<p>\n    \u306a\u3093\u3060\u304b\u826f\u304f\u308f\u304b\u308a\u307e\u305b\u3093\u304c\u3001ToString \u306f\u52d5\u304d\u307e\u305b\u3093&#8230;\u3063\u3066\u306a\u5177\u5408\u3067\u3059\u3002\n<\/p>\n<p>\n    \u3069\u3046\u3084\u3089\u3001\u300cLINQ to Entities\u300d\u3063\u3066\u306e\u304c\u52d5\u3044\u3066\u3044\u308b\u3088\u3046\u3067\u3001\u4ed5\u65b9\u304c\u306a\u3044\u300cLINQ to SQL\u300d\u3092\u52d5\u304b\u305d\u3046\u3002\u3068\u3044\u3046\u8a33\u3067\u3001\u4ed6\u306e\u65b9\u6cd5\u3067\u8a66\u3057\u3066\u307f\u307e\u3059\uff08\u3053\u308c\u3092\u540c\u3058\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306b\u7d44\u307f\u8fbc\u3082\u3046\u3068\u3057\u3066\u3001\u524d\u56de\u306e\u843d\u3068\u3057\u7a74\u306a\u3093\u3067\u3059\u306a\uff57\uff09\u3002\n<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npublic ActionResult StatesList()\r\n{\r\n    DataContext dc = getDataContext();\r\n    var items = from t in dc.GetTable&lt;TStates&gt;()\r\n                select new SelectListItem\r\n                {\r\n                    Value = t.id.ToString(),\r\n                    Text = t.name\r\n                };\r\n\r\n    ViewData&#x5B;&amp;quot;states&amp;quot;] = items;\r\n\r\n\r\n    return View();\r\n}\r\n<\/pre>\n<\/p>\n<p>\n    \u306e\u3088\u3046\u306b\u3001LINQ to SQL \u306e\u307b\u3046\u3092\u52d5\u304b\u3059\u3068\u3067\u3059\u306d\u3002\u3059\u3093\u306a\u308a\u52d5\u304d\u307e\u3059\u3002\u3046\uff5e\u3080\u3002\n<\/p>\n<p>\n    \u25bc\u753b\u50cf002\n<\/p>\n<p>\n    <a href=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2011\/01\/bw_uploads20110121_02.jpg\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" title=\"20110121_02.jpg\" border=\"0\" alt=\"20110121_02.jpg\" src=\"http:\/\/www.moonmile.net\/blog\/wp-content\/uploads\/2011\/01\/bw_uploadstm_20110121_02.jpg\" width=\"400\" height=\"293\" \/><\/a>\n<\/p>\n<p>\n    \u4f55\u51e6\u304c\u5f15\u3063\u639b\u304b\u3063\u3066\u3044\u308b\u306e\u304b\u3068\u3044\u3046\u3068\u3001ToString \u306a\u3093\u3067\u3059\u306d\u3002\u3069\u3046\u3084\u3089\u3001LINQ to Entities \u306e\u4e2d\u3067\u306f\u3001\u3053\u306e\u624b\u306e\u4ed6\u306e\u30e1\u30bd\u30c3\u30c9\u304c\u52d5\u304b\u306a\u3044\u3089\u3057\u3044\u3002\u3057\u304b\u3082\u3001LINQ \u306e\u5b9f\u884c\u304c\u9045\u5ef6\u3055\u308c\u308b\u306e\u3067(LINQ\u3092\u5b9a\u7fa9\u3057\u305f\u7b87\u6240\u3067\u306f\u306a\u304f\u3066\u5b9f\u969b\u306b\u5b9f\u884c\u3055\u308c\u308b\u5834\u6240\u3068\u3044\u3046\u610f\u5473\u3067)\u3001aspx \u5185\u3067\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u3068\u3044\u3046\u4e0d\u601d\u8b70\u306a\u5177\u5408\u306b\u306a\u308a\u307e\u3059\u3002\n<\/p>\n<p>\n    \u3044\u305a\u308c\u3001LINQ to SQL \u304c LINQ to Entities \u306b\u5168\u3066\u7f6e\u304d\u63db\u308f\u308b\u306e\u304b&#8230;\u306f\u4e0d\u660e\u306a\u306e\u3067\u3059\u304c\u3001\u3053\u3093\u306a\u98a8\u306b LINQ \u5185\u3067\u6587\u5b57\u5217\u51e6\u7406\u3092\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3061\u3087\u3063\u3068\u4e0d\u601d\u8b70\u306a\u73fe\u8c61\u306b\u51fa\u4f1a\u3044\u307e\u3059\u3002\n<\/p>\n<p>\n    \u306a\u306e\u3067\u3001LINQ to Entities \u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u306f\u3001\n<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\npublic ActionResult StatesList2()\r\n{\r\n    Models.mvcdbEntities ent = new Models.mvcdbEntities();\r\n    List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;();\r\n    foreach (var it in ent.TStates)\r\n    {\r\n        items.Add(new SelectListItem\r\n        {\r\n            Value = it.id.ToString(),\r\n            Text = it.name\r\n        });\r\n    }\r\n    ViewData&#x5B;&amp;quot;states&amp;quot;] = items;\r\n\r\n    return View();\r\n}\r\n<\/pre>\n<\/p>\n<p>\n    \u306e\u3088\u3046\u306b\u3001\u4e00\u5ea6 \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u4ee3\u5165\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3068\u3044\u3046&#8230;\u30c0\u30b5\u3044\u611f\u3058\u306b&#8230;\u3066\u306a\u308f\u3051\u3067\u9023\u8f09\u306e\u307b\u3046\u3067\u306f\u5374\u4e0b\u3057\u307e\u3057\u305f(\u82e6\u7b11)\u3002\u3053\u306e\u3042\u305f\u308a\u306e\u8aac\u660e\u304c\u4e3b\u76ee\u7684\u3067\u306f\u306a\u3044\u306e\u3067 orz\n<\/p>\n<p>\n    \u3055\u3066\u3001\u3053\u306e\u73fe\u8c61\u3067\u3059\u304c\u3001\u5b9f\u306f Visual Basic 2010 \u3067\u306f\u8d77\u3053\u308a\u307e\u305b\u3093\u3002\n<\/p>\n<p><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nFunction StatesList() As ActionResult\r\n    Dim ent = New mvcdbEntities\r\n    Dim items = From t In ent.TStates\r\n                Select New SelectListItem With {\r\n                    .Value = t.id,\r\n                    .Text = t.name\r\n                }\r\n\r\n    ViewData(&quot;states&quot;) = items\r\n\r\n    Return View()\r\nEnd Function\r\n<\/pre>\n<\/p>\n<p>\n    \u306e\u3088\u3046\u306b VB \u3092\u4f7f\u3063\u3066\u66f8\u304f\u3068\u3059\u3093\u306a\u308a\u52d5\u304d\u307e\u3059\u3002\n<\/p>\n<p>\n    \u3053\u308c\u306f\u3001ToString \u7b49\u306e\u5909\u63db\u3092\u4f7f\u3063\u3066\u3044\u306a\u3044\u304b\u3089\u306a\u3093\u3067\u3059\u306d\u3002.Value = t.id \u306e\u3068\u3053\u308d\u3067\u3001\u81ea\u52d5\u7684\u306b Integer \u304b\u3089 String \u306b\u5909\u63db\u3055\u308c\u3066\u3044\u308b\u308f\u3051\u3067\u3001\u3053\u308c\u304c\u3046\u307e\u304f\u52d5\u304f\u8981\u56e0\u306a\u306e\u3067\u3059\u3002C# \u306e\u5834\u5408\u306f\u3001\u81ea\u52d5\u5909\u63db\u304c\u52b9\u304b\u306a\u3044\u304b\u3089 ToString \u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3046\u3068\u3053\u308d\u3067\u30a2\u30a6\u30c8\u3001\u306a\u3093\u3067\u3059\u306a\u3041\u3002\n<\/p>\n<p>\n    \u3068\u3044\u3046\u308f\u3051\u3067\u3001\u540c\u3058 LINQ \u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u3067\u3082\u3001Entity Data Model(LINQ to Entities)\u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u3068\u3001LINQ to SQL \u3092\u4f7f\u3063\u3066\u3044\u308b\u5834\u5408\u3068\u3067\u533a\u5225\u3057\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u3068\u3046\u3001\u3061\u3087\u3063\u3068\u4e0d\u601d\u8b70\u306a\u843d\u3068\u3057\u7a74\uff08\u5b9f\u88c5\u304c\u9055\u3046\u3068\u3044\u308c\u3070\u3001\u308f\u304b\u308a\u3084\u3059\u3044\u3093\u3067\u3059\u304c\u3001\u30b3\u30fc\u30c9\u7684\u306b\u306f LINQ \u3060\u3051\u306a\u306e\u3067\u898b\u3064\u3051\u3065\u3089\u3044\uff09\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Entity Data Model\u3068LINQ to SQL \u306f\u540c\u6642\u306b\u4f7f\u3048\u306a\u3044 | Moonmile Solutions Blog http:\/\/www.moonmile.net\/blog\/archives\/1688 \u524d\u56de &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/1695\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19],"tags":[],"class_list":["post-1695","post","type-post","status-publish","format-standard","hentry","category-asp-net"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/1695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=1695"}],"version-history":[{"count":10,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/1695\/revisions"}],"predecessor-version":[{"id":1706,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/1695\/revisions\/1706"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=1695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=1695"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=1695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}