Skip to content

Treating System.Data.DataTable as enumerable clashes with member-access enumeration #6466

Closed

Description

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)

Activity

  1. MaximoTrinidad commented on Mar 22, 2018

    @MaximoTrinidad

    I am little confuse as I'm used to do this with SQL datasets:

    $sdt.rows.count 
    

    And, it will give me the number rows.

    Here's my SQL dataset sample:

    $getSvr = "systemname,1433";
    $sqlcon = "server=$Global:getSvr;database=Master;Integrated Security=false;User ID=sa;" + 'Password=$SqlPw0rd1!';
    
    ## - SQL Query:
    $SqlQry = @'
    sp_who2
    '@;
    
    ## - Create connection to SQL Server and execute the T-SQL command:
    $sda = New-Object System.Data.SqlClient.SqlDataAdapter ($SqlQry, $sqlcon);
    $sdt = New-Object System.Data.DataTable;
    $sda.fill($sdt) | Out-Null;
    
    ## - Display Results from SQLQuery:
    $sdt.rows
    
    ## - Total number of rows:
    $sdt.rows.count
    

    If it something else then apologies! Just trying to understand.
    :)

  2. mklement0 commented on Mar 22, 2018

    @mklement0
    ContributorAuthor

    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.

  3. MaximoTrinidad commented on Mar 22, 2018

    @MaximoTrinidad

    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!

    :)

  4. mklement0 commented on Mar 22, 2018

    @mklement0
    ContributorAuthor

    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 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).

  5. MaximoTrinidad commented on Mar 22, 2018

    @MaximoTrinidad

    Michael Klement (@mklement0),

    Gotcha!
    There's always the possibility it's going to come back ... By-Design! Hehe!
    :)

  6. added
    Issue-Discussion
    on Mar 23, 2018
  7. MaximoTrinidad commented on Mar 23, 2018

    @MaximoTrinidad

    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.

    :)

  8. mklement0 commented on Mar 23, 2018

    @mklement0
    ContributorAuthor

    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 .Count property.

    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:

      • give it a .Count property that reports the same thing as .Rows.Count
      • make it indexable so that [n] is the same thing as .Rows[n]
    • (b) Make it a well-behaved scalar:

      • give it a .Count property of 1 - it is, after all, one table (remember that something like (3).Count also yields 1 in v3+)
      • make it indexable as any scalar (since 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).

  9. mklement0 commented on Mar 24, 2018

    @mklement0
    ContributorAuthor

    Maximo Trinidad (@MaximoTrinidad):

    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).

  10. PetSerAl commented on Mar 30, 2018

    @PetSerAl
    Contributor

    Not every enumerable is a collection which have Count property and indexer.

    $a = [System.Linq.Enumerable]::Range(1,3)
    $a.Count
    $a[1].GetType()
  11. mklement0 commented on Mar 31, 2018

    @mklement0
    ContributorAuthor

    PetSerAl:

    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.

  12. PetSerAl commented on Mar 31, 2018

    @PetSerAl
    Contributor

    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.

  13. iSazonov commented on Mar 31, 2018

    @iSazonov
    Collaborator

    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.

  14. mklement0 commented on Mar 31, 2018

    @mklement0
    ContributorAuthor

    PetSerAl:

    Thanks for the background info, as usual.

    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().

    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.

  15. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  16. added
    Resolution-No Activity
    on Nov 16, 2023
  17. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  18. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  19. 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionResolution-No Activity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions