简体   繁体   English

通过转换为字符串将数据转换为数组

[英]Data to array via conversion to string

I have a dataset with one datatable, one row and six columns: 我有一个数据集,其中包含一个数据表,一行和六列:

DataSet holdall = new DataSet();

            holdone.Fill(holdall);

            DataTable invoiceTable = holdall.Tables["INVOICE"];

The data comes from an SQL database, and consists of several different data types. 数据来自SQL数据库,由几种不同的数据类型组成。 I need to pass this data into an array or a list (AFAIK An array is more appropriate), and on the way convert it into string format: 我需要将此数据传递到数组或列表中(AFAIK一个数组更合适),然后将其转换为字符串格式:

string[] currentInvoice = new string[6];
currentInvoice = invoiceTable.AsEnumerable().Take(1).ToString().ToArray();

This gives me the following error: 这给了我以下错误:

Cannot implicitly convert type 'char[]' to 'string[]' 无法将类型'char []'隐式转换为'string []'

I have also tried 我也尝试过

foreach (DataRow INVOICE in invoiceTable.AsEnumerable())
            {
                currentInvoice.ToArray();
            }

However, this returns a Null exception, that leads be to believe that there is no data in the datatable. 但是,这将返回Null异常,从而导致人们认为数据表中没有数据。 Upon examining the datatable via a stop point, I find that there is the correct amount of data. 通过停止点检查数据表后,我发现有正确数量的数据。

Am I overcomplicating this? 我使这个复杂化了吗?

When you call .ToString() you're converting whatever that is to a single string . 调用.ToString()时,您.ToString()任何内容转换为单个string Calling .ToArray() on a single string doesn't split that string into any meaningful substrings, it just converts it to a char[] , which as the error states is incompatible with the variable type string[] . 在单个字符串上调用.ToArray()不会将该字符串拆分为任何有意义的子字符串,而只是将其转换为char[] ,因为错误状态与变量类型string[]不兼容。

So, if I understand correctly... You have a table, of which you want only the first row, and you want the items in that row to be strings in an array? 所以,如果我理解正确的话...您有一个表,您只希望其中的第一行,并且希望该行中的项成为数组中的字符串? Something like this ought to work: 这样的事情应该起作用:

currentInvoice = invoiceTable.AsEnumerable().First().ItemArray.Select(i => i.ToString()).ToArray();

It takes the table as an enumerable, grabs the first row, looks at the array of items in that row, converts each item to a string , and converts that enumeration of string s into an array. 它将表作为可枚举,获取第一行,查看该行中的项目数组,将每个项目转换为string ,然后将string s的枚举转换为数组。

Edit: Note the use of .First() instead of .Take(1) . 编辑:请注意使用.First()而不是.Take(1) While conceptually they do the same thing, .Take() returns a collection of items (even if there's only one item in the collection) whereas .First() returns only a single item. 从概念上讲,它们执行相同的操作, .Take()返回一个项目的集合(即使该集合中只有一个项目),而.First()仅返回一个项目。 Note also that these could throw errors if there are no items in the collection from which you try to extract the first element. 还请注意,如果您尝试从中提取第一个元素的集合中没有任何项目,这些可能会引发错误。 So you might want to add some error checking to ensure the table has rows before trying to extract the first row, if there could be a situation where it doesn't have rows. 所以,你可能想添加一些错误检查,以确保该行试图提取的第一行,如果有可能的情况下它不具备行之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM