来自List< Row>的数据表 [英] Datatable from List<Row>

查看:147
本文介绍了来自List< Row>的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了List<Row> Rows中包含的数据,如下所示:

I have downloaded data that is contained in a List<Row> Rows like this:

class Row
{
    string[] Items { get; set; }
    public Row(string[] Items)
    {
        this.Items = Items;
    }
}

行基本上是逗号分隔的条目(.csv)

The rows are basically comma delimited entries (.csv)

using (var reader = new StreamReader(spreadSheetStream))          
{
    string header = reader.ReadLine(); //This is the header
    Rows.Add(new Row(header.Split(',')));

    while (!reader.EndOfStream)
    {
        string tickerInfo = reader.ReadLine();  //This is a data entry                                             
        Rows.Add(new Row(tickerInfo.Split(',')));                        
    }
}

我将List<Row>转换为Datatable这样

DataTable historicalDataTable = ToDataTable<Row>(Rows);

List<Row> Rows的第一个元素包含列名,其中七个.那么此后的每个元素都是一个实际的数据元素.

The first element of List<Row> Rows contains the names of the columns, seven of them. Then each element thereafter is an actual data element.

public static DataTable ToDataTable<T>(List<T> items)
{
    DataTable dataTable = new DataTable(typeof(T).Name);

    //Get all the properties
    PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    foreach (PropertyInfo prop in Props)
    {
        //Setting column names as Property names
        dataTable.Columns.Add(prop.Name);
    }
    foreach (T item in items)
    {
        var values = new object[Props.Length];
        for (int i = 0; i < Props.Length; i++)
        {
            //inserting property values to datatable rows
            values[i] = Props[i].GetValue(item, null);
        }
        dataTable.Rows.Add(values);
    }
    //put a breakpoint here and check datatable
    return dataTable;
}

当我尝试写出表的内容时,我看到正确的行数,但是ItemArray

When I try to write out the contents of the table, I see the right number of rows, but there is nothing in ItemArray

foreach (DataRow dataRow in historicalDataTable.Rows)
{
    Console.WriteLine(dataRow.ToString());
    foreach (var item in dataRow.ItemArray)
    {
        Console.WriteLine(item);
    }
}

推荐答案

您的代码有点矛盾.您正在尝试将属性复制为列名,但是您的csv代码实际上将第一行填充为列名.标题行和数据行之间没有区别

Your code is a bit contradictory. You are trying to copy properties as column names, however your csv code actually populates the first row as the column names. You have no distinction between header rows and data rows

您可以直接将其读入数据表,例如:-

You can just read it straight into a datatable with something like :-

(尽管您可能希望进行更好的错误检查)

(though you may want to do better error checking)

var dt = new DataTable("Rows");
string data = "a,b,c\r\n1,2,3\r\n4,5,6";
var stream = GenerateStreamFromString(data); // http://stackoverflow.com/questions/1879395/how-to-generate-a-stream-from-a-string

using (var reader = new StreamReader(stream))
{
    reader.ReadLine()?.Split(',').ToList().ForEach(h => dt.Columns.Add(h));
    while (!reader.EndOfStream)
    {
        dt.Rows.Add(reader.ReadLine()?.Split(',').ToArray());
    }
}
foreach (DataColumn dataColumn in dt.Columns)
{
    Console.Write($"{dataColumn.ColumnName} ");
}
Console.WriteLine();

foreach (DataRow dataRow in dt.Rows)
{
    Console.Write("Row: ");
    foreach (var item in dataRow.ItemArray)
    {
        Console.Write(item + " ");
    }
    Console.WriteLine();
}

这篇关于来自List&lt; Row&gt;的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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