{"id":359,"date":"2021-09-20T19:26:47","date_gmt":"2021-09-20T13:56:47","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=359"},"modified":"2021-09-20T19:26:47","modified_gmt":"2021-09-20T13:56:47","slug":"convert-object-to-datatable","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2021\/09\/20\/convert-object-to-datatable\/","title":{"rendered":"Convert Object to DataTable"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>public DataTable ToDataTable&lt;T>(List&lt;T> items)\n        {\n            DataTable dataTable = new DataTable(typeof(T).Name);\n            \/\/Get all the properties\n            PropertyInfo&#91;] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);\n            foreach (PropertyInfo prop in Props)\n            {\n                \/\/Setting column names as Property names\n                dataTable.Columns.Add(prop.Name);\n            }\n\n            foreach (T item in items)\n            {\n                var values = new object&#91;Props.Length];\n                for (int i = 0; i &lt; Props.Length; i++)\n                {\n                    \/\/inserting property values to datatable rows\n                    values&#91;i] = Props&#91;i].GetValue(item, null);\n                }\n                dataTable.Rows.Add(values);\n            }\n            \/\/put a breakpoint here and check datatable\n            return dataTable;\n        }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,4,2],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/359"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=359"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/359\/revisions"}],"predecessor-version":[{"id":360,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/359\/revisions\/360"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=359"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}