You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Treating System.Data.DataTable as enumerable clashes with member-access enumeration #6466
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or more
In the pipeline, [System.Data.DataTable] is still treated as a collection that implicitly enumerates its .Rows collection - through a mechanism that is unclear to me, given that it in other contexts this is currently, though possibly only temporarily, disabled (see #6453).
Given that [System.Data.DataTable]:
itself has no .Count property
is not itself indexable
the behavior with member-access enumeration - use of .Count and indexing - is not as expected:
Steps to reproduce
# Create a sample data table with 2 rows.1..2|% { $dt= [Data.DataTable]::new("test") } { $dt.Rows.Add($dt.NewRow()) }
# If we assume that [DataTable] is a collection, this should be equal to $dt.Rows.Count, i.e., 2$dt.Count# If we assume that [DataTable] is an indexable collection, this should return the# type name of the 2nd row, $dt.Rows[1], i.e., 'DataRow'$dt[1].GetType().Name
Expected behavior
2
DataRow
Note: If [DataTable] itself is NOT conceived of as a collection, the expected behavior would be:
1 # a single [DataTable] instance
You cannot call a method on a null-valued expression. # Error, because index 1 doesn't exist.
...
Actual behavior
1 # post-#5756 only: member enumeration was applied to the items of .Row
1 # ditto
You cannot call a method on a null-valued expression. # Error, index 1 was applied to $dt itself, not the .Rows collection
...
Note: $dt.Count returning 1, 1 is only seen with Ilya (@iSazonov)'s #5756 PR in effect, which has not been released yet as of PowerShell Core v6.0.2; without it, $null is output, because the row objects lack the implicit .Count property.
Environment data
PowerShell Core v6.0.2 on macOS 10.13.3
PowerShell Core v6.0.2 on Ubuntu 16.04.3 LTS
PowerShell Core v6.0.2 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Maximo Trinidad (@MaximoTrinidad): There is no issue if you're using the .Rows property explicitly - the problem I've described (without recommending a resolution, because I don't know the history) only surfaces if you use a DataTable instance itself as an enumerable.
Yes, it's been around for a long time, but it's PowerShell (both editions), not .NET (a C# program won't recognize a DataTable instance as enumerable with foreach, for example).
My aim was to document the awkwardness of the behavior.
Not sure what the outcome will be (document it and live with it / no longer treat DataTable as an enumerable).
I can't find any documentation that would tell that the property Count doesn't exist at the DataTable class. You'll find it available on the DataRowCollection Class.
This is why I was saying the issues can come from the .NET class and not PowerShell.
Now, you could probably add a patch in PowerShell to work around the issue but at up for the team to decide.
Now, you could probably add a patch in PowerShell to work around the issue but at up for the team to decide.
And, to be clear, it is PowerShell that is creating the problem to begin with, by treating the DataTable type itself as an enumerable, which it isn't intrinsically (as a .NET type).
Good point in general, but PowerShell is making DataTable act as if it were its own .Rows collection, which does have a .Count property and whose elements are indexable.
Therefore, I'd expect $someDataTable.Count to return the same as $someDataTable.Rows.Count and $someDataTable[$i] to return the same as $someDataTable.Rows[$i].
On a side note, $someDataTable.Rows[-1] returns $null rather than the last element.
As far as I understand all the magic about treating DataTable as enumerable is concealed in [System.Management.Automation.LanguagePrimitives]::GetEnumerable and [System.Management.Automation.LanguagePrimitives]::GetEnumerator. So, PowerShell never treat DataTable as alias to its Rows property, but merely enumeration of DataRows. Also, besides Count property and indexer DataRowCollection have other properties and methods, should them also be "copied" to DataTable? For example: $dt.GetEnumerator() fails now.
On a side note, $someDataTable.Rows[-1] returns $null rather than the last element.
If I understand it correctly, that is because DataRowCollection is not implement IList interface.
I think we could ask .Net Core team that is their plans to enhance DataTable with methods from Rows. If they like this we'll resolve the Issue automatically. If they don't like this we should ask PowerShell Committe about implementation this in PowerShell.
So, PowerShell never treat DataTable as alias to its Rows property, but merely enumeration of DataRows.
I guess what I'm saying is: it should, at least in terms of the features that matter for a consistent PowerShell user experience, and these features are a .Count property and the ability to index.
Personally, I'm not concerned about other members such as .GetEnumerator().
Since treating data tables themselves as enumerable is PowerShell's magic (which I think was unfortunate to begin with, but we're stuck with it now), I don't think the .NET Core team will be receptive to the idea.
I do hope the PowerShell committee will consider a PowerShell implementation, though.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 23, 2023
changed the title [-]Treating System.Data.DataTable as enumerable clashes with member enumeration[/-][+]Treating System.Data.DataTable as enumerable clashes with member-access enumeration[/+]on May 5, 2025
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or more
Activity
MaximoTrinidad commentedon Mar 22, 2018
I am little confuse as I'm used to do this with SQL datasets:
And, it will give me the number rows.
Here's my SQL dataset sample:
If it something else then apologies! Just trying to understand.
:)
mklement0 commentedon Mar 22, 2018
Maximo Trinidad (@MaximoTrinidad): There is no issue if you're using the
.Rowsproperty explicitly - the problem I've described (without recommending a resolution, because I don't know the history) only surfaces if you use aDataTableinstance itself as an enumerable.MaximoTrinidad commentedon Mar 22, 2018
Michael Klement (@mklement0),
It's been happening for the longest time so I got the habit to use the rows.count. I never thought about using it at the first instance.
I think this is another .NET issue not really PowerShell Core as it can be repro in Windows PowerShell.
That's my take!
:)
mklement0 commentedon Mar 22, 2018
Maximo Trinidad (@MaximoTrinidad):
Yes, it's been around for a long time, but it's PowerShell (both editions), not .NET (a C# program won't recognize a
DataTableinstance as enumerable withforeach, for example).My aim was to document the awkwardness of the behavior.
Not sure what the outcome will be (document it and live with it / no longer treat
DataTableas an enumerable).MaximoTrinidad commentedon Mar 22, 2018
Michael Klement (@mklement0),
Gotcha!
There's always the possibility it's going to come back ... By-Design! Hehe!
:)
iSazonov commentedon Mar 23, 2018
Michael Klement (@mklement0) I don't debug but seems it is here:
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/pipeline.cs#L1139
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/LanguagePrimitives.cs#L549
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/LanguagePrimitives.cs#L538
https://github.com/PowerShell/PowerShell/blob/master/src/System.Management.Automation/engine/runtime/Binding/Binders.cs#L595
MaximoTrinidad commentedon Mar 23, 2018
Ilya (@iSazonov)
Normally, when I look at issues like these, I search in MSDN Documentation for the .NET class having issues: https://msdn.microsoft.com/en-us/library/system.data.datatable(v=vs.100).aspx
I can't find any documentation that would tell that the property Count doesn't exist at the DataTable class. You'll find it available on the DataRowCollection Class.
This is why I was saying the issues can come from the .NET class and not PowerShell.
Now, you could probably add a patch in PowerShell to work around the issue but at up for the team to decide.
I understand Michael Klement (@mklement0) in making us aware about the issue.
:)
mklement0 commentedon Mar 23, 2018
Ilya (@iSazonov): Thanks for all the pointers, I'll take a look.
Maximo Trinidad (@MaximoTrinidad):
The issue I'm highlighting is unrelated to whether any properties of
[DataTable]instances contain objects with a.Countproperty.The issue is that a
[DataTable]instance half behaves like a - broken - collection and half like a - broken - scalar.This can be resolved in one of 2 ways:
(a) Make it a well-behaved collection:
.Countproperty that reports the same thing as.Rows.Count[n]is the same thing as.Rows[n](b) Make it a well-behaved scalar:
.Countproperty of1- it is, after all, one table (remember that something like(3).Countalso yields1in v3+)[0]and[-1]are no-ops (yield the object itself), any other index yields$null(a) is friendlier to backward-compatibility, but, at least from a conceptual perspective, my vote is for (b).
mklement0 commentedon Mar 24, 2018
Maximo Trinidad (@MaximoTrinidad):
And, to be clear, it is PowerShell that is creating the problem to begin with, by treating the
DataTabletype itself as an enumerable, which it isn't intrinsically (as a .NET type).PetSerAl commentedon Mar 30, 2018
Not every enumerable is a collection which have
Countproperty and indexer.mklement0 commentedon Mar 31, 2018
PetSerAl:
Good point in general, but PowerShell is making
DataTableact as if it were its own.Rowscollection, which does have a.Countproperty and whose elements are indexable.Therefore, I'd expect
$someDataTable.Countto return the same as$someDataTable.Rows.Countand$someDataTable[$i]to return the same as$someDataTable.Rows[$i].On a side note,
$someDataTable.Rows[-1]returns$nullrather than the last element.PetSerAl commentedon Mar 31, 2018
As far as I understand all the magic about treating
DataTableas enumerable is concealed in[System.Management.Automation.LanguagePrimitives]::GetEnumerableand[System.Management.Automation.LanguagePrimitives]::GetEnumerator. So, PowerShell never treatDataTableas alias to itsRowsproperty, but merely enumeration ofDataRows. Also, besidesCountproperty and indexerDataRowCollectionhave other properties and methods, should them also be "copied" toDataTable? For example:$dt.GetEnumerator()fails now.If I understand it correctly, that is because
DataRowCollectionis not implementIListinterface.iSazonov commentedon Mar 31, 2018
I think we could ask .Net Core team that is their plans to enhance
DataTablewith methods fromRows. If they like this we'll resolve the Issue automatically. If they don't like this we should ask PowerShell Committe about implementation this in PowerShell.mklement0 commentedon Mar 31, 2018
PetSerAl:
Thanks for the background info, as usual.
I guess what I'm saying is: it should, at least in terms of the features that matter for a consistent PowerShell user experience, and these features are a
.Countproperty and the ability to index.Personally, I'm not concerned about other members such as
.GetEnumerator().Ilya (@iSazonov):
Since treating data tables themselves as enumerable is PowerShell's magic (which I think was unfortunate to begin with, but we're stuck with it now), I don't think the .NET Core team will be receptive to the idea.
I do hope the PowerShell committee will consider a PowerShell implementation, though.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.
[-]Treating System.Data.DataTable as enumerable clashes with member enumeration[/-][+]Treating System.Data.DataTable as enumerable clashes with member-access enumeration[/+]