为什么这个演员阵容不起作用?

发布于 2024-11-28 19:06:52 字数 632 浏览 0 评论 0原文

此代码有效:

foreach(DataColumn column in table.Columns)
{
    // do whatever
}

但此代码无效:

(IEnumerable<DataColumn>)(table.Columns)

.Columns 属性返回 DataColumnCollection 这是一个 InternalDataCollectionBase,实现了 IEnumerable,所以它应该可以工作。

我得到的错误是

无法将类型“System.Data.DataColumnCollection”转换为“System.Collections.Generic.IEnumerable”

This code works:

foreach(DataColumn column in table.Columns)
{
    // do whatever
}

But this code doesn't:

(IEnumerable<DataColumn>)(table.Columns)

The .Columns property returns a DataColumnCollection which is an InternalDataCollectionBase, which implements IEnumerable, so it should work.

The error I get is

Cannot convert type 'System.Data.DataColumnCollection' to 'System.Collections.Generic.IEnumerable'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧城空念 2024-12-05 19:06:52

DataColumnCollection 实现 IEnumerable,并且每个返回的行一个 DataColumn,但它没有实现 IEnumerable< ;数据列>。由于它没有实现该接口,因此您无法转换为该接口。由于类是密封的,编译器知道该值不可能实现该接口,因此您甚至无法在编译时对其进行强制转换。

使用已安装的 LINQ Cast 方法:

table.Columns.Cast<DataColumn>()

这实际上是一个适配器方法 - 当您从结果中获取列集合中的每个元素时,它都会被延迟地转换为 DataColumn

foreach 编译的原因是编译器为您添加了显式强制转换。例如,这将编译

foreach (string x in table.Columns)
{
}

...但它会在执行时抛出异常。

DataColumnCollection implements IEnumerable, and each returned row is a DataColumn, but it doesn't implement IEnumerable<DataColumn>. As it doesn't implement the interface, you can't cast to the interface. As the class is sealed, the compiler knows that the value can't possibly implement the interface, so you can't even cast to it at compile-time.

Use the LINQ Cast method insted:

table.Columns.Cast<DataColumn>()

That's effectively an adapter method - each element in the column collection will be lazily cast to DataColumn as you fetch it from the result.

The reason the foreach compiles is that the compiler adds an explicit cast for you. For instance, this will compile:

foreach (string x in table.Columns)
{
}

... but it'll throw an exception at execution time.

假扮的天使 2024-12-05 19:06:52

这是因为 DataColumnCollection 实现了 IEnumerable ,但没有实现 IEnumerable

var columns = (IEnumerable)(table.Columns) 应该可以工作

IEnumerable 和 IEnumerable 没有任何关系。 (然而 IEnumerable 继承自 IEnumerable,它可以使用动态鸭子类型化为 IEnumerable,但它并没有真正帮助你(即使它有很多)有趣))

It's because DataColumnCollection implements IEnumerable, but not IEnumerable<T> .

var columns = (IEnumerable)(table.Columns) should work

IEnumerable and IEnumerable aren't related in any way. (however IEnumerable<DataColumn> inherits from IEnumerable<object> which could be duck typed to IEnumerable using dynamic, but it doesn't really help you (even if it's a lot of fun) )

在梵高的星空下 2024-12-05 19:06:52

在第一种情况下,您每次都会在 Columns 集合中单独转换大小写,但在第二种情况下,您会立即转换整个集合。这实际上与:

// Cats implements IAnimal
Cats[] cats = new Cats[20]; 
...
IAnimal animal = cats[0];

// this you cannot do
Cats[] cats = new Cats[20]; 
IAnimal[] animal = cats;

然而,它没有实际意义,因为你可以这样做:

table.Columns.Cast<DataColumn>()

并完成它。请注意,使用 Cast<> 比简单地获取通用 IEnumerable 更好,因为保留了强类型。

In the first case you are individually casing each time within the Columns collection, but in the second you are casting the entire collection at once. It's really no different than:

// Cats implements IAnimal
Cats[] cats = new Cats[20]; 
...
IAnimal animal = cats[0];

versus

// this you cannot do
Cats[] cats = new Cats[20]; 
IAnimal[] animal = cats;

However, it is moot, as you can do this:

table.Columns.Cast<DataColumn>()

And be done with it. Notice that using the Cast<> is preferable to simply getting a generic IEnumerable as the strong typing is preserved.

老旧海报 2024-12-05 19:06:52

这是因为 DataColumnCollection 仅实现 IEnumerable,因此您无法将其转换为通用版本 IEnumerable。使用 foreach 时,编译器按照约定对每个元素进行转换。

This is because DataColumnCollection only implements IEnumerable, so you cannot cast it to the generic version IEnumerable<DataColumn>. When using foreach, the compiler does the casting for you on each element by convention.

破晓 2024-12-05 19:06:52

试试这个:

var columns = table.Columns as IEnumerable;

Try this:

var columns = table.Columns as IEnumerable;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文