如何在不迭代的情况下将 DataRow 数组转换为 DataTable?

发布于 2024-08-26 20:09:03 字数 43 浏览 6 评论 0原文

如何在不迭代的情况下将 DataRow 数组转换为 DataTable?

How can I convert a DataRow array to a DataTable without iteration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦回梦里 2024-09-02 20:09:03

您可以使用:

dataTable = datarowarray.CopyToDataTable()

但请确保 datarowarray 具有 length > 1,否则最终会出现不需要的异常。

You can use:

dataTable = datarowarray.CopyToDataTable()

but ensure that datarowarray has length > 1, otherwise it will end up with unwanted exceptions.

我认为您不能将数据行数组放入数据表中。您可以使用 DataTable.ImportRow 一次导入一行。

foreach(DataRow row in dataRowArray)
{
   dataTable.ImportRow(row);
}

I dont think you can put an array of datarows to a datatable. You can import one row at a time using DataTable.ImportRow.

foreach(DataRow row in dataRowArray)
{
   dataTable.ImportRow(row);
}
凹づ凸ル 2024-09-02 20:09:03

即使确实存在像

myDataTable.LoadRows(dataRowArray)

...这样的 .NET 框架函数,所做的只是隐藏迭代。该框架并没有神奇地规避迭代步骤(尽管在某些情况下它可能会做一些非常聪明的事情来优化它)。

Even if there did exist a .NET framework function like

myDataTable.LoadRows(dataRowArray)

... all that would do is hide the iteration. The framework doesn't magically circumvent the iterative step (though in some cases it might be doing something gee-whiz smart to optimize it).

你与清晨阳光 2024-09-02 20:09:03

建议使用.NET框架的一些批量功能以获得更好的性能。

DataTable dtDestination = new DataTable();  //your destination datatable

DataRow[] row;
row = dtMain.Select($"unit IN ({ filterString })"); //select data from main datatable

if (row.Length == 0)
{
    //handle if nothing match your select statment.
}
else
{
    //if have found row.
    dtDestination = row.CopyToDataTable();

    //or if your datatable is in DataSet and it's readonly.
    DataTable dtTemp = row.CopyToDataTable();
    dts.Tables["labofpatient"].Merge(dtTemp);
}

这是另一种循环导入的方法,但它比上面的函数更慢并且花费更多的时间。

DataTable dtDestination = new DataTable();  //your destination datatable

DataRow[] row;
row = dtMain.Select($"unit IN ({ filterString })"); //select data from main datatable

foreach (DataRow dr in row)
{
    //you can choose one of the line of code below. It work on the same and no different in performance.
    dtDestination.Rows.Add(dr.ItemArray);   
    dtDestination.ImportRow(dr);
}

或通过迭代数据表中的值

DataTable dtDestination = new DataTable();  //your destination datatable
for(int i = 0; i < dt.Rows.count; i++)
{
    dtDestination.Rows.Add(dt.Rows[i]["col1"].ToString(), dt.Rows[i]["col2"].ToString());
}

Recommend to use some bulk function of .NET framework for better performance.

DataTable dtDestination = new DataTable();  //your destination datatable

DataRow[] row;
row = dtMain.Select(
quot;unit IN ({ filterString })"); //select data from main datatable

if (row.Length == 0)
{
    //handle if nothing match your select statment.
}
else
{
    //if have found row.
    dtDestination = row.CopyToDataTable();

    //or if your datatable is in DataSet and it's readonly.
    DataTable dtTemp = row.CopyToDataTable();
    dts.Tables["labofpatient"].Merge(dtTemp);
}

This is another way to import by loop, but it's slower and take time more than above function.

DataTable dtDestination = new DataTable();  //your destination datatable

DataRow[] row;
row = dtMain.Select(
quot;unit IN ({ filterString })"); //select data from main datatable

foreach (DataRow dr in row)
{
    //you can choose one of the line of code below. It work on the same and no different in performance.
    dtDestination.Rows.Add(dr.ItemArray);   
    dtDestination.ImportRow(dr);
}

or by iterate value in datatable

DataTable dtDestination = new DataTable();  //your destination datatable
for(int i = 0; i < dt.Rows.count; i++)
{
    dtDestination.Rows.Add(dt.Rows[i]["col1"].ToString(), dt.Rows[i]["col2"].ToString());
}
落叶缤纷 2024-09-02 20:09:03
    DataTable dt = new DataTable();
    DataRow[] dataRowArray = dt.Select("");
    DataTable dataTable = new DataTable();
    foreach (DataRow row in dataRowArray)
    {
        dataTable = dataTable.Clone();
        dataTable.ImportRow(row);
    }
    DataTable dt = new DataTable();
    DataRow[] dataRowArray = dt.Select("");
    DataTable dataTable = new DataTable();
    foreach (DataRow row in dataRowArray)
    {
        dataTable = dataTable.Clone();
        dataTable.ImportRow(row);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文