Title: Extend [DataTable] with easy constructor and AddRow method · Issue #11987 · PowerShell/PowerShell · GitHub
Open Graph Title: Extend [DataTable] with easy constructor and AddRow method · Issue #11987 · PowerShell/PowerShell
X Title: Extend [DataTable] with easy constructor and AddRow method · Issue #11987 · PowerShell/PowerShell
Description: A list of [PSCustomObject] objects consumes multiple times the amount of memory as a [DataTable]. For a lot of .Net classes there are accelerators and PowerShell constructors but to create a [DataTable] in PowerShell is quiet an hassle, ...
Open Graph Description: A list of [PSCustomObject] objects consumes multiple times the amount of memory as a [DataTable]. For a lot of .Net classes there are accelerators and PowerShell constructors but to create a [DataT...
X Description: A list of [PSCustomObject] objects consumes multiple times the amount of memory as a [DataTable]. For a lot of .Net classes there are accelerators and PowerShell constructors but to create a [DataT...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/11987
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Extend [DataTable] with easy constructor and AddRow method","articleBody":"A list of `[PSCustomObject]` objects consumes multiple times the amount of memory as a `[DataTable]`. For a lot of .Net classes there are accelerators and PowerShell constructors but to create a `[DataTable]` in PowerShell is quiet an hassle, see e.g. [Creating table using Powershell](https://docs.microsoft.com/nl-nl/archive/blogs/rkramesh/creating-table-using-powershell).\r\n\r\nIt would be nice to be able to construct a data table the PowerShell way, e.g.:\r\n```PowerShell\r\n$Table = [DataTable]('Text', 'Number')\r\n```\r\nOr:\r\n```PowerShell\r\n$Table = [DataTable]@{Text = [String]; 'Number' = [Int]}\r\n```\r\n\r\nAnd add data rows like:\r\n```PowerShell\r\n$Table.AddRow(@('A', 1))\r\n```\r\nOr:\r\n```PowerShell\r\n$Table.AddRow(@{Text = 'A', Number = 1})\r\n```\r\nAs I don't have enough experience with C#, I have created a derived `[DataTable]` class in \r\nPowerShell to test the idea:\r\n\r\n```PowerShell\r\nclass DataTable : Data.DataTable {\r\n\tDataTable([Array]$ColumnNames) {\r\n\t\tForEach ($ColumnName in $ColumnNames) { \r\n\t\t\tIf ($ColumnName -is [PSCustomObject]) {Write-Host 'PSCustomObject'}\r\n\t\t\tElse { $This.AddColumn($ColumnName) }\r\n\t\t}\r\n\t}\r\n\tDataTable([System.Collections.IDictionary]$ColumnNameTypes) {\r\n\t\tForEach($Key in $ColumnNameTypes.Get_keys()) { $This.AddColumn($Key, $ColumnNameTypes[$Key]) }\r\n\t}\r\n\t[Void]AddColumn([String]$ColumnName) {\r\n\t\t$DataColumn = New-Object Data.DataColumn($ColumnName)\r\n\t\t$This.Columns.Add($DataColumn)\r\n\t}\r\n\t[Void]AddColumn([String]$ColumnName, [Type]$ColumnType = [String]) {\r\n\t\t$DataColumn = New-Object Data.DataColumn($ColumnName, $ColumnType)\r\n\t\t$This.Columns.Add($DataColumn)\r\n\t}\r\n\t[Void]AddRow([Array]$Array) {\r\n\t\t$DataRow = $This.NewRow()\r\n\t\tFor ($i = 0; $i -lt $Array.Length; $i++) {\r\n\t\t\tIf ($i -lt $This.Columns.Count) { $DataRow.Item($i) = $Array[$i] }\r\n\t\t}\r\n\t\t$This.Rows.Add($DataRow)\r\n\t}\r\n\t[Void]AddRow([System.Collections.IDictionary]$Dictionary) {\r\n\t\t$DataRow = $This.NewRow()\r\n\t\tForEach ($Name in $Dictionary.get_Keys()) {\r\n\t\t\tIf ($This.Columns.Contains($Name)) { $DataRow.Item($Name) = $Dictionary[$Name] }\r\n\t\t}\r\n\t\t$This.Rows.Add($DataRow)\r\n\t}\r\n\t[Void]AddRow([PSCustomObject]$PSObject) {\r\n\t\t$DataRow = $This.NewRow()\r\n\t\tForEach ($Name in $PSObject.PSObject.Properties.Name) {\r\n\t\t\tIf ($This.Columns.Contains($Name)) { $DataRow.Item($Name) = $PSObject.$Name }\r\n\t\t}\r\n\t\t$This.Rows.Add($DataRow)\r\n\t}\r\n}\r\n\r\n#Example 1\r\n\r\n$MyDataTable = [DataTable]('a', 'b', 'c')\r\n$MyDataTable.AddRow(@(1,2,3))\r\n$MyDataTable\r\n\r\na b c\r\n- - -\r\n1 2 3\r\n\r\n#Example 2\r\n\r\n$Table = [DataTable]@{Text = [String]; 'Number' = [Int]}\r\n$Table.AddRow(@{Text = 'A'; Number = 1})\r\n$Table\r\n\r\nText Number\r\n---- ------\r\nA 1\r\n\r\n# Example 3\r\n\r\n# $Process = [DataTable]@((Get-Process | Select-Object -First 1).PSStandardMembers.DefaultDisplayPropertySet.ReferencedPropertyNames)\r\n$Process = [DataTable][Ordered]@{Id = [Int]; ProcessName = [String]}\r\nGet-Process | ForEach-Object { $Process.AddRow($_) }\r\n$Process","author":{"url":"https://github.com/iRon7","@type":"Person","name":"iRon7"},"datePublished":"2020-03-02T11:13:14Z","url":"https://github.com/PowerShell/PowerShell/issues/11987"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:96c85738-106c-8320-9792-d0123c43485e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B6CE:22AC9A:2483AE:319C26:6AFD51ED |
| html-safe-nonce | 9ffc8c58ea61f479a644a11eee630daaf9137ad031637abf2a3e9f97a819b66f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCNkNFOjIyQUM5QToyNDgzQUU6MzE5QzI2OjZBRkQ1MUVEIiwidmlzaXRvcl9pZCI6IjM3NDMxNTU5ODEyNzg1OTc2MTMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | a8f54556d7a9833aa231490b1fc119a123a3ad39357a6f2cd5c0f23ffd7c2eba |
| hovercard-subject-tag | issue:573902024 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/11987/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f86fdaefe20e4a7561a410572bd49a903fa127261c6723a06ac1586c05ff8aa0/PowerShell/PowerShell/issues/11987 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f86fdaefe20e4a7561a410572bd49a903fa127261c6723a06ac1586c05ff8aa0/PowerShell/PowerShell/issues/11987 |
| og:image:alt | A list of [PSCustomObject] objects consumes multiple times the amount of memory as a [DataTable]. For a lot of .Net classes there are accelerators and PowerShell constructors but to create a [DataT... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | iRon7 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 66653c2e55a6fff2930be1bdf07ee4f8aa137e3956d056c822563be6f878755e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | b6f01f7cb9f35f8c09949773241a3d49dd04b340 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width