{"id":2056,"date":"2011-02-23T00:52:41","date_gmt":"2011-02-22T15:52:41","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/archives\/2056"},"modified":"2011-02-23T00:53:19","modified_gmt":"2011-02-22T15:53:19","slug":"%e3%83%87%e3%83%bc%e3%82%bf%e3%82%b3%e3%83%b3%e3%83%90%e3%83%bc%e3%83%88%e3%81%ab-explicit-%e3%82%92%e4%bd%bf%e3%81%86","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/2056","title":{"rendered":"\u30c7\u30fc\u30bf\u30b3\u30f3\u30d0\u30fc\u30c8\u306b explicit \u3092\u4f7f\u3046"},"content":{"rendered":"<p>\nC#\u306e\u3042\u307e\u308a\u77e5\u3089\u308c\u3066\u3044\u306a\u3044\u6a5f\u80fd\u30b7\u30ea\u30fc\u30ba\u3063\u3066\u3053\u3068\u3067\u3001explicit \u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u30b3\u30f3\u30d0\u30fc\u30c8\u3057\u307e\u3057\u3087\u3046\u3063\u3066\u306e\u3092\u8a66\u3057\u3066\u307f\u307e\u3059\u3002\u3068\u8a00\u3044\u307e\u3059\u304b\u3001<b>linq to xml \u3044\u3089\u306a\u3044\u3088\u3082\u3069\u304d<\/b>\u3092\u4f5c\u3063\u3066\u3044\u308b\u6700\u4e2d\u306b\u3001\u305d\u3046\u3044\u3048\u3070\u30ad\u30e3\u30b9\u30c8\u3082\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\u304c\u3067\u304d\u305f\u3088\u306a\u3041\u3001\u3063\u3066\u3068\u3053\u308d\u3067\u6c17\u3065\u304d\u307e\u3057\u305f\u3002\n<\/p>\n<p>\n\u4e00\u822c\u7684\u306a\u578b\u306e\u30b3\u30f3\u30d0\u30fc\u30c8\u306f\u2193\u306e\u3088\u3046\u306b\u3059\u308b\u306e\u304c\u666e\u901a\u306a\u306e\u3067\u3059\u304c&#8230;\n<\/p>\n<p>\n\u578b\u306a\u3057DataTable\u304b\u3089\u578b\u4ed8\u304dDataTable\u306b\u30b3\u30d4\u30fc\u3059\u308b\u65b9\u6cd5 | Moonmile Solutions Blog<br \/>\n<a href=\"http:\/\/www.moonmile.net\/blog\/archives\/2047\">http:\/\/www.moonmile.net\/blog\/archives\/2047<\/a>\n<\/p>\n<p>\n\u3044\u3061\u3044\u3061\u3001\u4f55\u3068\u304b.CONV \u95a2\u6570\u3092\u4f5c\u308b\u306e\u304c\u3046\u3056\u3063\u305f\u3044\u3067\u3059\u3088\u306d\u3002\u3068\u8a00\u3046\u304b\u3001\u4f7f\u3046\u3068\u304d\u306b Conv \u3057\u3066\u3044\u308b\u306e\u304c\u3001\u3061\u3087\u3063\u3068\u3063\u3066\u306a\u611f\u3058\u306a\u306e\u3067\u3059\u3002\u5909\u63db\u3057\u3066\u3044\u308b\u306e\u304c\u5206\u304b\u308a\u6613\u3044\u3068\u3044\u3048\u3070\u5206\u304b\u308a\u6613\u3044\u306e\u3067\u3059\u304c\u3001C++ \u98a8\u306b\u8a00\u3048\u3070\u3001\u578b\u304b\u3089\u578b\u3078<b>\u30ad\u30e3\u30b9\u30c8<\/b>\u3063\u3066\u3044\u3046\u306e\u3082\u660e\u793a\u7684\u306a\u5909\u63db\u306a\u8a33\u3067\u3057\u3066\u3001\u305d\u308c\u3092\u5229\u7528\u3057\u307e\u3059\u3002\n<\/p>\n<p>\n\u307e\u305a\u306f\u3001\u4f7f\u3044\u65b9\u304b\u3089\n<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\nprivate void button3_Click(object sender, EventArgs e)<br \/>\n{<br \/>\n\t\/\/ \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u63a5\u7d9a<br \/>\n\tSqlConnection cn = new SqlConnection(@&quot;Data Source=.\\sqlexpress;Initial Catalog=mvcdb;Integrated Security=True;MultipleActiveResultSets=True&quot;);<br \/>\n\tcn.Open();<br \/>\n\tDataTable dt = new DataTable();<br \/>\n\tSqlDataAdapter da = new SqlDataAdapter(<br \/>\n\t\t&quot;SELECT * FROM TProduct&quot;, cn);<br \/>\n\tda.Fill(dt);<br \/>\n\t\/\/ \u30ad\u30e3\u30b9\u30c8\u3059\u308b<br \/>\n\tDataTableProduct product = (DataTableProduct)dt; \/\/ \u2605<br \/>\n\tdataGridView1.DataSource = product;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\n\u4f55\u3092\u3084\u3063\u3066\u3044\u308b\u306e\u304b\u3001\u3044\u307e\u3044\u3061\u4e0d\u660e\u3067\u3059\u304c\uff57\u3001\u2605\u306e\u3068\u3053\u308d\u3067\u3001DataTable \u304b\u3089 \u578b\u4ed8\u306e DataTableProduct \u306b\u5909\u63db\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u306e\u3068\u3053\u308d\u3001\u666e\u901a\u306f converet \u95a2\u6570\u3092\u4f5c\u308b\u3068\u3053\u308d\u306a\u306e\u3067\u3059\u304c\u3001\u3053\u3093\u306a\u98a8\u306b\u660e\u793a\u7684\u306a\u30ad\u30e3\u30b9\u30c8(explicit)\u3092\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\n<\/p>\n<p>\n# \u3061\u306a\u307f\u306b\u6697\u9ed9\u306e\u30ad\u30e3\u30b9\u30c8(implicit)\u3092\u4f7f\u3046\u3053\u3068\u3082\u3067\u304d\u308b\u306e\u3067\u3059\u304c\u3001\u3053\u306e\u5834\u5408\u306f convert \u3059\u308b\u306e\u304c\u660e\u78ba\u306b\u306a\u3063\u305f\u307b\u3046\u304c\u3044\u3044\u304b\u306a\u3068\u3002\n<\/p>\n<p>\n\u305d\u3057\u3066\u3001\u3053\u308c\u3092\u5b9f\u73fe\u3057\u3066\u3044\u308b\u306e\u304c\u2193\u306e\u4ed5\u7d44\u307f\u3067\u3059\u3002\n<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<br \/>\npublic class DataRowProduct<br \/>\n{<br \/>\n\t\/\/ \u30d7\u30ed\u30d1\u30c6\u30a3<br \/>\n\tpublic string id { get; set; }<br \/>\n\tpublic string name { get; set; }<br \/>\n\tpublic int price { get; set; }<\/p>\n<p>\t\/\/ \u30ad\u30e3\u30b9\u30c8\u3067\u5909\u63db<br \/>\n\tpublic static explicit operator DataRowProduct(DataRow row)<br \/>\n\t{<br \/>\n\t\tDataRowProduct dest = new DataRowProduct();<br \/>\n\t\tforeach (PropertyInfo pi in dest.GetType().GetProperties())<br \/>\n\t\t{<br \/>\n\t\t\tpi.SetValue( dest, Convert.ChangeType(row[pi.Name], pi.PropertyType),null);<br \/>\n\t\t}<br \/>\n\t\treturn dest;<br \/>\n\t}<br \/>\n}<br \/>\npublic class DataTableProduct : IListSource<br \/>\n{<br \/>\n\tprotected List&lt;DataRowProduct&gt; _rows = new List&lt;DataRowProduct&gt;();<br \/>\n\tpublic List&lt;DataRowProduct&gt; Rows<br \/>\n\t{<br \/>\n\t\tget { return _rows; }<br \/>\n\t}<\/p>\n<p>\tpublic bool ContainsListCollection<br \/>\n\t{<br \/>\n\t\tget { return true; }<br \/>\n\t}<\/p>\n<p>\tpublic System.Collections.IList GetList()<br \/>\n\t{<br \/>\n\t\treturn this.Rows;<br \/>\n\t}<\/p>\n<p>\t\/\/ \u30ad\u30e3\u30b9\u30c8\u3067\u5909\u63db<br \/>\n\tpublic static implicit operator DataTableProduct(DataTable dt)<br \/>\n\t{<br \/>\n\t\tDataTableProduct dest = new DataTableProduct();<br \/>\n\t\tforeach (DataRow row in dt.Rows)<br \/>\n\t\t{<br \/>\n\t\t\tdest.Rows.Add((DataRowProduct)row);<br \/>\n\t\t}<br \/>\n\t\treturn dest;<br \/>\n\t}<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>\nDataRowProduct \u30af\u30e9\u30b9\u306e\u307b\u3046\u3067\u306f\u3001\u30ea\u30d5\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u306e\u8a70\u3081\u8fbc\u307f\u3001\u305d\u3057\u3066\u3001DataTableProduct \u306e\u307b\u3046\u306f\u3001\u30ad\u30e3\u30b9\u30c8\u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf Rows \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u305f\u3081\u8fbc\u3093\u3067\u3044\u307e\u3059\u3002\u5b9f\u306f\u3001\u3053\u306e convert \u306e\u6642\u306b\u30b3\u30d4\u30fc\u304c\u767a\u751f\u3057\u3066\u3044\u308b\u306e\u3067\u3001\u3042\u3093\u307e\u308a\u3046\u307e\u304f\u306a\u3044\u3093\u3067\u3059\u3088\u306d\u3002\u3069\u3061\u3089\u304b\u3068\u3044\u3048\u3070\u3001Rows \u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306e\u30a2\u30af\u30bb\u30b9\u306e\u305f\u3073\u306b DataTable.Rows \u304b\u3089\u5f15\u3063\u5f35\u3063\u3066\u304f\u308b\u3063\u3066\u3044\u3046\u6280\u306e\u307b\u3046\u304c\u826f\u3044\u304b\u306a\u3041\u3068\u3082\u601d\u3044\u307e\u3059\u3002\n<\/p>\n<p>\n\u307e\u3041\u3001\u305d\u308c\u306f\u514e\u3082\u89d2\u3068\u3057\u3066\u3001\u30ad\u30e3\u30b9\u30c8\u3082\u3046\u307e\u304f\u4f7f\u3046\u3068<b>\u5909\u63db<\/b>\u306e\u610f\u5473\u304c\u8868\u305b\u308b\u304b\u3089\u4fbf\u5229\u3063\u3066\u306a\u8a71\u3067\u3059\u3002<br \/>\n\u30ad\u30e3\u30b9\u30c8\u3068 convert \u95a2\u6570\u306e\u9055\u3044\u306f\u3001\n<\/p>\n<ul>\n<li>\u30ad\u30e3\u30b9\u30c8\u306e\u307b\u3046\u306f\u3001\u5909\u63db\u5148\u304c\u6ce8\u76ee\u3055\u308c\u308b\u3002<\/li>\n<li>convert \u95a2\u6570\u306e\u307b\u3046\u306f\u3001\u5909\u63db\u5143\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/li>\n<li>\u3042\u308b\u3044\u306f\u3001convert \u95a2\u6570\u306f\u3001\u5909\u63db\u5148\u306b from \u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3002<\/li>\n<\/ul>\n<p>\n\u3068\u3044\u3046\u9055\u3044\u304c\u3042\u308a\u307e\u3059\u3002\u5177\u4f53\u7684\u306b\u30b3\u30fc\u30c9\u3067\u793a\u3057\u3066\u307f\u308b\u3068\u3001\n<\/p>\n<p>[code lang=&#8221;csharp&#8221;]<\/p>\n<p>DataTable dt ;<br \/>\n\/\/ \u30ad\u30e3\u30b9\u30c8\u306e\u5834\u5408\u306f\u3001\u5de6\u5411\u304d\u304c\u660e\u78ba\u306b\u306a\u308b\u3002<br \/>\n\/\/ \u5909\u63db\u5143\u306f\u8868\u9762\u4e0a\u306f\u51fa\u3066\u3053\u306a\u3044<br \/>\nDataTableProduct table = (DataTableProduct)dt;<\/p>\n<p>\/\/ \u5909\u63db\u5143\u3067\u8ffd\u52a0\u3059\u308b\u30b3\u30f3\u30d0\u30fc\u30c8\u95a2\u6570<br \/>\n\/\/ ToInteger \u306a\u3093\u3066\u306e\u304c\u305d\u3046\u3002\u4eca\u56de\u306e\u5834\u5408\u306f\u3001DataTable \u3092\u89e6\u308c\u306a\u3044\u306e\u3067\u7121\u7406\u3002<br \/>\n\/\/ \u5b9f\u306f\u3001C# \u306e\u62e1\u5f35\u30e1\u30bd\u30c3\u30c9\u3092\u5229\u7528\u3059\u308b\u3068\u5b9f\u73fe\u3067\u304d\u305f\u308a\u3002<br \/>\nDataTableProduct table = dt.ToDataTableProduct();<\/p>\n<p>\/\/ \u5909\u63db\u5148\u3067\u8ffd\u52a0\u3059\u308b\u30b3\u30f3\u30d0\u30fc\u30c8\u95a2\u6570<br \/>\n\/\/ ParseInt \u306a\u3093\u3066\u306e\u304c\u305d\u3046\u3002<br \/>\n\/\/ From \u30e1\u30bd\u30c3\u30c9\u304b\u3001Conv \u30e1\u30bd\u30c3\u30c9\u304b\u547d\u540d\u306b\u8ff7\u3046\u3068\u3053\u308d<br \/>\nDataTableProduct table = DataTableProduct.From(dt);<\/p>\n<p>[\/code]<\/p>\n<p>\n\u3053\u3093\u306a\u611f\u3058\u306b\u3059\u308b\u3068\u3001<b>\u30ad\u30e3\u30b9\u30c8<\/b>\u306e\u610f\u5473\u304c\u5206\u304b\u3063\u3066\u3044\u308c\u3070\u3001\u30ad\u30e3\u30b9\u30c8\u306e\u307b\u3046\u304c\u81ea\u7136\u304b\u306a\u3001\u3068\u601d\u3063\u305f\u308a\u3057\u307e\u3059\u3002DataTable \u3082 DataTableProduct \u3082\u4f3c\u305f\u611f\u3058\u306e\u30af\u30e9\u30b9\u306a\u8a33\u3067\u3059\u304b\u3089\u3001\u578b\u304b\u3089\u578b\u3078\u306e\u5909\u63db\u3001\u3068\u3044\u3046\u307b\u3046\u304c\u81ea\u7136\u306b\u898b\u3048\u307e\u3059\u3002\u307e\u3041\u3001\u30b7\u30f3\u30bf\u30c3\u30af\u30b9\u30b7\u30e5\u30ac\u30fc\u3063\u307d\u3044\u3068\u3053\u308d\u306f\u3042\u308a\u307e\u3059\u304c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C#\u306e\u3042\u307e\u308a\u77e5\u3089\u308c\u3066\u3044\u306a\u3044\u6a5f\u80fd\u30b7\u30ea\u30fc\u30ba\u3063\u3066\u3053\u3068\u3067\u3001explicit \u3092\u4f7f\u3063\u3066\u30c7\u30fc\u30bf\u30b3\u30f3\u30d0\u30fc\u30c8\u3057\u307e\u3057\u3087\u3046\u3063\u3066\u306e\u3092\u8a66\u3057\u3066\u307f\u307e\u3059\u3002\u3068\u8a00\u3044\u307e\u3059\u304b\u3001linq to xml \u3044\u3089\u306a\u3044\u3088\u3082\u3069\u304d\u3092\u4f5c\u3063\u3066\u3044\u308b\u6700\u4e2d\u306b\u3001\u305d\u3046\u3044\u3048\u3070\u30ad\u30e3\u30b9\u30c8\u3082\u30aa &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/2056\">\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":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[23],"tags":[],"class_list":["post-2056","post","type-post","status-publish","format-standard","hentry","category-csharp"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2056","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=2056"}],"version-history":[{"count":1,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2056\/revisions"}],"predecessor-version":[{"id":2057,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/2056\/revisions\/2057"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=2056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=2056"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=2056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}