【问题标题】:Runtime error on casting Array to string Enumerator [duplicate]将数组转换为字符串枚举器时出现运行时错误[重复]
【发布时间】:2013-12-02 10:24:50
【问题描述】:

这是Design issues and implementing Enumerable.AsEnumerable<FarPoint.Win.Spread.Row>的后续问题

我已将我的 farpoint 行转换为 2 d 对象数组,如下所示:

object[,] nthRow = fpSpread2.ActiveSheet.GetArray(e.Row, e.Column, 1, FarPointSpread1.ActiveSheet.ColumnCount);

我尝试将此转换为字符串,以便我可以应用 Linq,但在运行时出现以下错误:

IEnumerator<string> narry = (IEnumerator<string>)nthRow.GetEnumerator();

无法将“ArrayEnumerator”类型的对象转换为“System.Collections.Generic.IEnumerator`1[System.String]”类型。

我该如何解决这个问题?

【问题讨论】:

  • @riezebosch - 它不回答二维数组?
  • 如果数据中有多个列,您希望返回哪些数据?连接文本?

标签: c# linq enumeration farpoint-spread


【解决方案1】:

即使对于多维数组,您也可以在其上调用Cast&lt;T&gt;

using System.Linq;
...
nthRow.Cast<string>().GetEnumerator()    // returns IEnumerator<string>

但数组中的每个元素实际上都应该是类型string。如果你想做像ToString()这样的格式化,你可以先将它们转换成object,然后调用Select&lt;T&gt;

【讨论】:

  • 我只需要一个从第 0 个索引开始的一维数组我怎么能得到它?
  • @Lohit 在给定多维数组的情况下,在 C# 中无法轻松完成。您必须复制数组元素。但是,如果你只想要一行,为什么在调用fpSpread2.ActiveSheet.GetArray时不指定行号
猜你喜欢
  • 2012-11-30
  • 2021-03-18
  • 2019-02-21
  • 2012-12-07
  • 1970-01-01
  • 1970-01-01
  • 2014-09-27
相关资源
最近更新 更多