[英]Remove rows from a DataTable
I need to remove rows from a DataTable dt where a column have a specific value. 我需要从列具有特定值的DataTable dt中删除行。 Before I remove them I copy them to a other DataTable dt2nd. 在删除它们之前,我将它们复制到另一个DataTable dt2nd。
It looks like this 看起来像这样
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
if (row["Column2"].ToString() == "This Value")
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
}
if (row["Column2"].ToString() == "Other Value")
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
}
//This continues with more if
}
But I get a error message 但是我收到一条错误消息
"Additional information: This row has been removed from a table and does not have any data. BeginEdit() will allow creation of new data in this row.". “附加信息:此行已从表中删除,并且没有任何数据.BeginEdit()将允许在此行中创建新数据。”
Lots of rows can have the "This Value" or "Other Value". 很多行可以具有“此值”或“其他值”。
The ==" " values can be a lot of different values so if I do it like this it will be a lot of IF statements. ==“”值可以是很多不同的值,所以如果我这样做,那将是很多IF语句。
Is there any good way to do this, with LINQ or some other way? 有没有什么好方法可以用LINQ或其他方式做到这一点?
This code will work : 此代码将起作用:
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
if (row["Column2"].ToString() == "This Value")
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
continue;
}
if (row["Column2"].ToString() == "Other Value")
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
continue;
}
//This continues with more if
}
or you can use a switch statement instead of all the ifs or use else if 或者您可以使用switch语句而不是所有ifs或使用else if
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
if (row["Column2"].ToString() == "This Value")
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
}
else if (row["Column2"].ToString() == "Other Value")
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
}
//This continues with more else if
}
or 要么
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
switch(row["Column2"].ToString()){
case "This Value":
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
break;
}
case "Other Value":
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
break;
}
//This continues with more case
}
But you can also use a select on your datatable and iflter out all the rows that need to be moved out. 但是,您也可以在数据表上使用select,并输出需要移出的所有行。 something like this : 这样的事情:
dt.select("Column2 == 'This value' or ...");
it will return the rows that satisfy the condition. 它将返回满足条件的行。 See example here : http://www.dotnetperls.com/datatable-select 请参见此处的示例: http : //www.dotnetperls.com/datatable-select
But the code can be made a lot shorter if the action is always the same : 但是如果操作总是相同的话,代码可以缩短很多:
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
if (
(row["Column2"].ToString() == "This Value") ||
(row["Column2"].ToString() == "Other Value") //more checks
)
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
}
}
or 要么
for (int i = dt.Rows.Count - 1; i >= 0; i--)
{
DataRow row = dt.Rows[i];
switch(row["Column2"].ToString()){
case "This Value":
case "Other Value": //more case values
{
dt2nd.ImportRow(row); //Copy
dt.Rows.Remove(row); //Remove
break;
}
}
You can try this... 你可以尝试这个......
var checkValues = new string[] { "Some value1", "Some value3" };
DataTable dt = new DataTable();
DataTable dt1 = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
dt.Rows.Add(new string[] {"Some value1", "Some value2", "Some value3" });
dt.AsEnumerable().ToList().ForEach(x =>
{
if (checkValues.Contains(x["Column1"]))
{
dt1.ImportRow(x);
dt.Rows.Remove(x);
}
});
var valuesToBeChecked = new String[] { "value1", "value2" };
var x = from y in dt.AsEnumerable()
where valuesToBeChecked.Any(t => valuesToBeChecked.Contains(y["Column2"].ToString()))
select y;
var z = dt.AsEnumerable().Except(x);
Then, you can copy z
to datatable
you want: 然后,您可以复制z
到datatable
你想要的:
DataTable dt2 = z.CopyToDataTable();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.