I have datatable1 which has 11 columns and 100,000 rows.. I would like to do a check to see if the text in column one starts with "one" and if it does, add that row into the second datatable. I have done the below but yet still it does not work.. I get the error that the row belong to another table
我有datatable1有11列和100,000行..我想检查一下第一列中的文本是否以“one”开头,如果是,则将该行添加到第二个数据表中。我已经完成了以下但仍然无法正常工作..我得到该行属于另一个表的错误
foreach (DataRow r in queryDataTable.Rows)
{
if (r[0].ToString().StartsWith(queryString))
{
dt.ImportRow(r);
}
}
2 个解决方案
#1
1
You cannot directly import the row of one table into another datatable. You need to create a new row and then copy the row.
您不能直接将一个表的行导入另一个数据表。您需要创建一个新行,然后复制该行。
Try this -
尝试这个 -
dt.Rows.Add(r.ItemArray)
#2
1
Instead of your for loop, you may use LINQ to select those rows which StartsWith queryString
and then you can use CopytoDataTable method to create a new table for the selected rows.
您可以使用LINQ来选择StartsWith queryString的那些行,而不是for循环,然后您可以使用CopytoDataTable方法为所选行创建一个新表。
var NewTable = queryDataTable.AsEnumerable()
.Where(r => r.Field<string>(0).StartsWith(queryString))
.CopyToDataTable();
Remember to include using System.Linq;
at the top.
记得包括使用System.Linq;在顶部。
#1
1
You cannot directly import the row of one table into another datatable. You need to create a new row and then copy the row.
您不能直接将一个表的行导入另一个数据表。您需要创建一个新行,然后复制该行。
Try this -
尝试这个 -
dt.Rows.Add(r.ItemArray)
#2
1
Instead of your for loop, you may use LINQ to select those rows which StartsWith queryString
and then you can use CopytoDataTable method to create a new table for the selected rows.
您可以使用LINQ来选择StartsWith queryString的那些行,而不是for循环,然后您可以使用CopytoDataTable方法为所选行创建一个新表。
var NewTable = queryDataTable.AsEnumerable()
.Where(r => r.Field<string>(0).StartsWith(queryString))
.CopyToDataTable();
Remember to include using System.Linq;
at the top.
记得包括使用System.Linq;在顶部。