Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将arraylist转换为字符串[]?_C#_Asp.net_Arraylist - Fatal编程技术网

C# 如何将arraylist转换为字符串[]?

C# 如何将arraylist转换为字符串[]?,c#,asp.net,arraylist,C#,Asp.net,Arraylist,可能重复: 如何将数组列表转换为string[]?下面是我正在尝试的代码。我得到一个错误: 源数组中至少有一个元素无法向下转换为目标数组类型 你试过这个吗: string[] array = myArrayList.ToArray(typeof(string)) as string[]; 你试过这个吗: string[] array = myArrayList.ToArray(typeof(string)) as string[]; 您正在将许多DataRow对象放入ArrayList中数

可能重复:

如何将数组列表转换为
string[]
?下面是我正在尝试的代码。我得到一个错误:


源数组中至少有一个元素无法向下转换为目标数组类型

你试过这个吗:

string[] array = myArrayList.ToArray(typeof(string)) as string[];
你试过这个吗:

string[] array = myArrayList.ToArray(typeof(string)) as string[];

您正在将许多
DataRow
对象放入
ArrayList
中<代码>数据行s不是字符串s。所以:那不行。充其量,您也许可以得到
.ToString()
,但坦率地说,这并没有多大帮助,因为每一行都会说
System.Data.DataRow
(因为它不会覆盖
ToString()
。我认为您需要重新评估您正在尝试做的事情

基于评论讨论的示例:

DataTable table = ...
string[] strings = new string[table.Rows.Count];
int idx = 0;
foreach(DataRow row in table.Rows)
{
    StringBuilder sb = new StringBuilder();
    object[] cells = row.ItemArray;
    for(int i = 0 ; i < cells.Length ; i++)
    {
        if (i != 0) sb.Append(',');
        sb.Append('"').Append(cells[i]).Append('"');
    }
    strings[idx++] = sb.ToString();
}
DataTable=。。。
string[]strings=新字符串[table.Rows.Count];
int-idx=0;
foreach(table.Rows中的DataRow行)
{
StringBuilder sb=新的StringBuilder();
object[]cells=row.ItemArray;
for(int i=0;i
您正在将许多
DataRow
对象放入
ArrayList
DataRow
s不是
string
s。因此:这不起作用。您最多可以得到
.ToString()
,但坦率地说,这不会有多大帮助-因为每一行只会说
System.Data.DataRow
(因为它不会覆盖
ToString()
。我认为您需要重新评估您正在尝试做的事情

基于评论讨论的示例:

DataTable table = ...
string[] strings = new string[table.Rows.Count];
int idx = 0;
foreach(DataRow row in table.Rows)
{
    StringBuilder sb = new StringBuilder();
    object[] cells = row.ItemArray;
    for(int i = 0 ; i < cells.Length ; i++)
    {
        if (i != 0) sb.Append(',');
        sb.Append('"').Append(cells[i]).Append('"');
    }
    strings[idx++] = sb.ToString();
}
DataTable=。。。
string[]strings=新字符串[table.Rows.Count];
int-idx=0;
foreach(table.Rows中的DataRow行)
{
StringBuilder sb=新的StringBuilder();
object[]cells=row.ItemArray;
for(int i=0;i
您有一个
数组列表
,其元素类型为
数据行
。您试图将其转换为
字符串
的数组。一个
数据行
不是
字符串
(向下转换时也不会变为一个),因此您不能这样做

如果字符串数组是您最终想要的,那么在填充
数组列表时,您可能应该对每个元素调用
ToString
(或任何您需要的转换)


(此外,正如Tim Schmelter在评论中所说,您应该真正为某些
T
使用
列表,可能是
字符串
数据行
,这取决于您的代码最终的外观,而不是
数组列表
)您有一个
数组列表
,其元素类型为
数据行
。您试图将其转换为一个
字符串
数组。数据行
不是
字符串
(向下转换时也不会变成一个),因此您不能这样做

如果字符串数组是您最终想要的,那么在填充
数组列表时,您可能应该对每个元素调用
ToString
(或任何您需要的转换)

(另外,正如Tim Schmelter在评论中所说,你真的应该为一些
T
使用
列表
——可能是
字符串
数据行
,这取决于你的代码最终看起来如何——而不是
数组列表

你不能使用数据行(类型)并将其添加到ArrayList中,希望它是字符串。 DataRow是一个对象,它拥有大量关于该对象以及对象数据的信息

foreach(DataRow dtRow in dtSet.Tables[0].Rows)
{
    myArrayList.Add(dtRow["UserName"].ToString());
}

string[] myArray = myArrayList.ToArray(typeof(string));
不能接受数据行(类型)并将其添加到ArrayList中,而将其预期为字符串。 DataRow是一个对象,它拥有大量关于该对象以及对象数据的信息

foreach(DataRow dtRow in dtSet.Tables[0].Rows)
{
    myArrayList.Add(dtRow["UserName"].ToString());
}

string[] myArray = myArrayList.ToArray(typeof(string));

arrayList中的每个项都是DataRow类型,不能将其强制转换为字符串

相反,您应该构造一个表示行内容的字符串,并将该字符串添加到列表中,例如

myArrayList.Add(dtRow["CustomerName"]);

arrayList中的每个项都是DataRow类型,不能将其强制转换为字符串

相反,您应该构造一个表示行内容的字符串,并将该字符串添加到列表中,例如

myArrayList.Add(dtRow["CustomerName"]);

您还在使用.NET 1.1吗?或者为什么不使用键入的
List
而不是
ArrayList

首先,您不能将
数据行
强制转换为
字符串
。您可以强制转换为字符串

如果可以使用,则可以使用以下代码获取所有行字段的
字符串[]
,其中每个字段用逗号分隔:

String[] rowFields = DtSet.Tables[0]
                    .AsEnumerable()
                    .Select(r => string.Join(",", r.ItemArray));
                    .ToArray();

您还在使用.NET 1.1吗?或者为什么不使用键入的
List
而不是
ArrayList

首先,您不能将
数据行
强制转换为
字符串
。您可以强制转换为字符串

如果可以使用,则可以使用以下代码获取所有行字段的
字符串[]
,其中每个字段用逗号分隔:

String[] rowFields = DtSet.Tables[0]
                    .AsEnumerable()
                    .Select(r => string.Join(",", r.ItemArray));
                    .ToArray();
输出:

as
as
as
as
as
as
输出:

as
as
as
as
as
as

您是否仍在使用.NET 1.1。或者为什么不使用键入的
List
而不是
ArrayList
?“源数组中至少有一个元素无法向下转换为目标数组类型。”它确实告诉您这里的问题……有点像;源数组中的每个“元素”是一个
DataRow
。它告诉您它不能将
DataRow
强制转换为
string
。这是完全正确的;
DataRow
不是
string
。您正在尝试将数据行强制转换为字符串。您需要首先检查您拥有的行是否允许这样做。如果允许,您可以简单地循环并将它们分别大小写并添加到字符串数组中。但我怀疑此转换是否会成功。@Shaks:DataRows是neve