Hi Friends,
In this post we are going to Filter a DataTable using LINQ.
We can filter a DataTable in many ways but using LINQ is the best and recommended approach..
In the below code snippet we are going to filter the datatable and then assign the results back to it.
Below are the assumptions before you jump into the code.
var tempTable = from DataRow dataRow in dtMainTable.Rows
where dataRow["ColumnName"] == "Value"
select dataRow;
if (tempTable.Count() > 0)
{
dtMainTable= tempTable.CopyToDataTable();
dtMainTable.AcceptChanges();
}
Hope this helps...
In this post we are going to Filter a DataTable using LINQ.
We can filter a DataTable in many ways but using LINQ is the best and recommended approach..
In the below code snippet we are going to filter the datatable and then assign the results back to it.
Below are the assumptions before you jump into the code.
- dtMainTable is the DataTable which you got it from your logic.
var tempTable = from DataRow dataRow in dtMainTable.Rows
where dataRow["ColumnName"] == "Value"
select dataRow;
if (tempTable.Count() > 0)
{
dtMainTable= tempTable.CopyToDataTable();
dtMainTable.AcceptChanges();
}
Hope this helps...