简体   繁体   English

什么是将DataTable转换为对象的最有效方法[,]?

[英]What's the most efficient way to convert a DataTable to an object[,]?

I have a bunch of DataTables that need to be converted to object[,] arrays ( not object[][] arrays). 我有一堆DataTables需要转换为object[,]数组( 而不是 object[][]数组)。 What's the most efficient way to do this in terms of performance? 在性能方面,最有效的方法是什么?

I know I can do this by building my object[dt.Rows.Count, dt.Columns.Count] initially, then looping through rows and parsing each row into a place in the array, but I'm fairly sure there's other methods such as using Linq or System.Data-specific features such as dataRow.ToItemArray() which may be more efficient. 我知道我可以通过最初构建我的object[dt.Rows.Count, dt.Columns.Count]来做到这一点,然后循环遍历行并将每行解析到数组中的某个位置,但我很确定还有其他方法如此使用Linq或System.Data特有的功能,如dataRow.ToItemArray()可能更有效。

My DataTables are of variable sizes, and contain both Dates and Numbers which need to be formatted appropriately, in addition to strings. 我的DataTables具有可变大小,除了字符串之外,还包含需要格式化的日期和数字。

For example, if one of my data tables contained 例如,如果包含我的一个数据表

Id    Name    Date                 Value
1     Rachel  1/1/2013 00:00:00    100.0000
2     Joseph  3/31/2012 00:00:00   50.0000
3     Sarah   2/28/2013 00:00:00   75.5000

then I would want an object[,] array containing the exact same data (ideally with headers), but with formatted Dates and Values 那么我想要一个包含完全相同数据的object[,]数组(理想情况下是标题),但是需要格式化的日期和值

arr[x,0] = row[x].Field<int>("Id");
arr[x,1] = row[x].Field<string>("Name");
arr[x,2] = row[x].Field<DateTime>("Date").ToString("M/d/yy");
arr[x,3] = row[x].Field<decimal>("Value").ToString("C2"); // Currency format

Basically we need: 基本上我们需要:

  1. Allocate memory for object[,] object[,]分配内存

    We cannot do much here.. we need to ensure we allocate memory once and not re-allocate it again. 我们在这里做不了多少..我们需要确保一次分配内存而不是再次重新分配。 So it's obvious we need create array at once, without using operations like List.Add(...) which internally re-allocate memory blocks. 因此很明显我们需要一次创建数组,而不使用像内部重新分配内存块的List.Add(...)这样的操作。

  2. Then, we need to copy objects from row items into multidimensional array. 然后,我们需要将对象从行项复制到多维数组中。 We could not use Buffer.BlockCopy here as we work with objects. 我们在处理对象时无法使用Buffer.BlockCopy Naturally we can not rely on any memcpy -like behavior, as CLR for each object needs either copy its reference, or do unbox->copy in heap->box for value types. 当然,我们不能依赖任何类似memcpy的行为,因为每个对象的CLR需要复制其引用,或者对值类型执行unbox-> copy in heap-> box。 So, the simplest way will be just for.. for.. style. 所以,最简单的方法就是... ..风格。

So, looks like most performance solution here is the intuitive one: 所以,看起来这里的大多数性能解决方案都是直观的:

public static object[,] Convert(DataTable dt)
{
    var rows = dt.Rows;
    int rowCount = rows.Count;
    int colCount = dt.Columns.Count;
    var result = new object[rowCount, colCount];

    for (int i = 0; i < rowCount; i++)
    {
        var row = rows[i];
        for (int j = 0; j < colCount; j++)
        {
            result[i, j] = row[j];
        }
    }

    return result;
}

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

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