Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Thursday, January 12, 2012

How to filter a DataTable using LINQ

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.
  • 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...

Popular Posts