为什么LINQ与Data.DataTableCollection铸造 [英] Why LINQ casting with a Data.DataTableCollection

查看:132
本文介绍了为什么LINQ与Data.DataTableCollection铸造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这问题是LINQ与Data.DataTableCollection铸造连续的问题,这里是链接[问题]的 LINQ与Data.DataTableCollection @Lasse Espeholt铸造



我情况相同的情况。我想对收集DataTableCollection中的LINQ查询。但是如果我写下下面的代码:

 从dataSet.Tables 
的dataTable

在Visual Studio将报告错误找不到源类型System.Data.DataTableCollection查询模式的实现。因此,解决方法是更改​​代码以下列内容:

 从dataTable的在dataSet.Tables.Cast<&数据表GT; ()



所以我挖从互联网上的根本原因。而且我发现了一些线索,一个典型的LINQ到对象查询表达式不仅需要实现IEnumerable作为数据源的类,但它也会返回此相同类型的实例。在此页面。所以我想DataTableCollection中不继承IEnumerable接口。但根据我在MSDN检查,我发现DataTableCollection中的的传承关系如下:

  DataTableCollection中
InternalDataCollectionBase
ICollection的
IEnumerable的

所以这个的传承关系意味着DataTableCollection中只有继承IEnumerable接口,不继承IEnumerable接口。因此它不能支持LINQ查询。并将该溶液用流延法的变换IEnumerable和然后可以做LINQ查询



因此​​,我的作为类摘要其中要启用的LINQ到对象的功能,它继承了这两个IEnumerable接口和IEnumerable



是我的总结吗?什么是IEnumerable和IEnumerable的之间的区别?


解决方案

这里的问题是, DataTableCollection中未实现普通的的IEnumerable< T> 接口。此接口添加了.NET 2.0,并与来自非通用继承了IEnumerable 接口,已经有从成立之初。 LINQ查询需要这个通用接口,并且不与非通用版本。



DataTableCollection中是类2.0在这之前已经可用,而普通的的IEnumerable< T> 添加在.NET 2.0



尽管微软可以添加上existig框架类型此通用接口,我的猜测是,他们没有因为时间的限制,风险和有限的用处不是。



有很多老班,可以从有这个通用接口(和它有时是很烦人的,他们缺少的接口)中获益,但添加这需要很多的时间来实施和测试的向后兼容性。改变现有的东西,包括打破现有的应用程序的风险。在.NET设计师真的保守引入重大更改。



所以我认为他们计算出在哪里效益比更高,因为那些老的类型,将不被使用的费用在很多新的工具的可用性的未来。此外,有一个简单的解决办法在 Enumerable.Cast<的形式,T>(可枚举)扩展方法,你已经找到



C#的LINQ语言实现,甚至还内置了这种支持演员LT; T> 方法;它可以如下写:

 从dataSet.Tables 
DataTable中的dataTable
<从dataTable的

 :/ pre> 

这在幕后转化为在dataSet.Tables.Cast<&数据表GT;()


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 Espeholt

I case the same situation. I want to do LINQ query on collection DataTableCollection. But if I write down the following code:

from dataTable in dataSet.Tables

The visual studio will report a error "Could not find an implementation of the query pattern for source type 'System.Data.DataTableCollection'". So the solution is change the code to following:

from dataTable in dataSet.Tables.Cast<DataTable> ()

So I dig the the root cause from internet. And I found some clue as "A typical LINQ to Objects query expression not only takes a class that implements IEnumerable as its data source, but it also returns an instance of this same type." in this page. So I wonder DataTableCollection don't inherit the interface IEnumerable. But according I checking in MSDN, I found the inheritence relationship of DataTableCollection is as following:

DataTableCollection
    InternalDataCollectionBase
        ICollection
            IEnumerable

So this inheritence relationship means that DataTableCollection only inherit the interface IEnumerable, don't inherit the interface IEnumerable. So it can't support the LINQ query. And the solution is using the cast method the transform to IEnumerable and then can do LINQ query.

So my summary as the class which want to enable the functionality of LINQ to object, it has to inherit the both interface IEnumerable and IEnumerable.

Is my summary right? And what's the difference between IEnumerable and IEnumerable?

解决方案

The problem here is that DataTableCollection doesn't implement the generic IEnumerable<T> 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<T> 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<T>(Enumerable) extension method, which you already found.

The C# LINQ language implementation even has built-in support for this Cast<T> 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<DataTable>()

这篇关于为什么LINQ与Data.DataTableCollection铸造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆