Why LINQ casting with a Data.DataTableCollection

后端 未结 1 1870
情话喂你
情话喂你 2021-01-20 12:17

This question is a continuous question of \"LINQ casting with a Data.DataTableCollection\", here is the link [question]LINQ casting with a Data.DataTableCollection @Lasse Es

1条回答
  •  有刺的猬
    2021-01-20 12:41

    The problem here is that DataTableCollection doesn't implement the generic IEnumerable interface. This interface is added with .NET 2.0 and it inherits from the non-generic IEnumerable interface that has been there from the early days. LINQ queries need this generic interface and don't work with the non-generic version.

    DataTableCollection is a class that has been available before 2.0 while the generic IEnumerable was added in .NET 2.0.

    Although Microsoft could have added this generic interface on existig framework types, my guess is that they didn't because of time constraints, risks and limited usefulness.

    There are a lot of old classes that could benefit from having this generic interface (and it is sometimes quite annoying that they lack that interface), but adding this takes a lot of time to implement and test for backwards compatibility. Changing existing stuff includes the risk of breaking existing applications. The .NET designers are really conservative about introducing breaking changes.

    So I think they calculated that the costs where higher than the benefits, because those old types would not be used that much in the future of the availability of new tools. Besides, there is an easy workaround in the form of the Enumerable.Cast(Enumerable) extension method, which you already found.

    The C# LINQ language implementation even has built-in support for this Cast method; it can be written as follows:

    from DataTable dataTable in dataSet.Tables
    

    Which under the covers translates to:

    from dataTable in dataSet.Tables.Cast()
    

    0 讨论(0)
提交回复
热议问题