I have a DataTable available with me which contains thousands of rows. There is a column called EmpID which is containing '0' for some of the rows. I want to remove them from my current DataTable and want to create a new correct DataTable. I cannot go row by row checking it since it contains huge amount of data. Give me a suggestion to overcome this problem.
我有一个DataTable,包含数千行。有一个名为EmpID的列,其中包含某些行的“0”。我想从我当前的DataTable中删除它们,并希望创建一个新的正确的DataTable。我不能逐行检查它,因为它包含大量数据。给我一个克服这个问题的建议。
4 个解决方案
#1
7
the best way would be to filter it at source (if possible) - so if you are creating it from a db, exclude all 0 values in your sql query itself using a where
最好的方法是在源代码中过滤它(如果可能的话) - 所以如果你是从db创建的,那么使用where在你的sql查询中排除所有0值
starting .net 2.0, ms enhanced the filtering logic on the datatable to a great extent. so if you used the dataview (on top of your datatable) and added the where clause in there and added some sort of runtime indexes on this field, it would give you the desired results without looping over all records
启动.net 2.0,ms在很大程度上增强了数据表上的过滤逻辑。因此,如果您使用了数据视图(在数据表之上)并在其中添加了where子句并在此字段上添加了某种运行时索引,它将为您提供所需的结果,而无需遍历所有记录
#2
1
You can use DataTable.Select("EmpID <> 0")
. This will return an array of DataRows which you can create your new DataTable from if required.
您可以使用DataTable.Select(“EmpID <> 0”)。这将返回一个DataRows数组,您可以根据需要创建新的DataTable。
#3
1
Isn't it possible to first select the rows with EmpID = 0 and then iterate over these only ?
是不是可以首先选择EmpID = 0的行,然后只迭代这些行?
DataTable newTable = new DataTable();
foreach (DataRow dr in oldTable.Select("EmpID = '0'")) {
newTable.Rows.Add(dr);
oldTable.Rows.Remove(dr);
}
#4
0
You can try
你可以试试
DataRow[] temp= table.Select("EmpID ='0'");
DataRow [] temp = table.Select(“EmpID ='0'”);
foreach(DataRow dr in temp) { table.Rows.Remove(dr); }
table.acceptchanges();
#1
7
the best way would be to filter it at source (if possible) - so if you are creating it from a db, exclude all 0 values in your sql query itself using a where
最好的方法是在源代码中过滤它(如果可能的话) - 所以如果你是从db创建的,那么使用where在你的sql查询中排除所有0值
starting .net 2.0, ms enhanced the filtering logic on the datatable to a great extent. so if you used the dataview (on top of your datatable) and added the where clause in there and added some sort of runtime indexes on this field, it would give you the desired results without looping over all records
启动.net 2.0,ms在很大程度上增强了数据表上的过滤逻辑。因此,如果您使用了数据视图(在数据表之上)并在其中添加了where子句并在此字段上添加了某种运行时索引,它将为您提供所需的结果,而无需遍历所有记录
#2
1
You can use DataTable.Select("EmpID <> 0")
. This will return an array of DataRows which you can create your new DataTable from if required.
您可以使用DataTable.Select(“EmpID <> 0”)。这将返回一个DataRows数组,您可以根据需要创建新的DataTable。
#3
1
Isn't it possible to first select the rows with EmpID = 0 and then iterate over these only ?
是不是可以首先选择EmpID = 0的行,然后只迭代这些行?
DataTable newTable = new DataTable();
foreach (DataRow dr in oldTable.Select("EmpID = '0'")) {
newTable.Rows.Add(dr);
oldTable.Rows.Remove(dr);
}
#4
0
You can try
你可以试试
DataRow[] temp= table.Select("EmpID ='0'");
DataRow [] temp = table.Select(“EmpID ='0'”);
foreach(DataRow dr in temp) { table.Rows.Remove(dr); }
table.acceptchanges();