Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can filter a dataTable with Linq to datatable?

hi how i can filter a datatable with linq to datatable? I have a DropDownList and there I can select the value of the Modul Column. Now I want to filter the DataTable with this Modul Column.

here is my datatable structure:

User | Host | TimeDiff | License | Telefon | Modul 

Here the Code:

protected void drp_Modules_SelectedIndexChanged(object sender, EventArgs e)
{
    string value = drp_Modules.SelectedValue;

    DataTable tb = (DataTable)Session["dt_Users"];

    tb = from item in tb //?????

    LoadUsertable(tb);
}
like image 261
Tarasov Avatar asked Oct 18 '13 12:10

Tarasov


People also ask

How do you filter data in a DataTable?

With the DataTable. Select() method, you can directly assign filter rows to an array or data table using this expression. Create a type variable, either DataTable or DataRow [], that is an array of Data Rows. By default, this method returns the array of data rows but you can convert it at any time to a Data Table.

Can we use LINQ to query against a DataTable?

Can we use linq to query against a DataTable? Explanation: We cannot use query against the DataTable's Rows collection, since DataRowCollection doesn't implement IEnumerable<T>. We need to use the AsEnumerable() extension for DataTable.

What is the LINQ query operator used to filter data?

Filtering operators are those operators which are used to filter the data according to the user requirement from the given data source or from the given sequence. For example, in an employee record, we want to get the data of the employees whose age in 21.


3 Answers

You are better of using DataTable.Select method, but if you have to use LINQ then you can try:

DataTable selectedTable = tb.AsEnumerable()
                            .Where(r => r.Field<string>("Modul") == value)
                            .CopyToDataTable();

This would create a new DataTable based on filtered values.

If you use DataTable.Select

string expression = "Modul =" + value;
DataRow[] selectedRows = tb.Select(expression);
like image 142
Habib Avatar answered Oct 08 '22 18:10

Habib


You can use condition to check rows exist in addition before casting. System.Linq namespace is required for Any() to work

var rows = values.AsEnumerable().Where
            (row => row.Field<string>("Status") == action);//get the rows where the status is equal to action

if(rows.Any())
{
    DataTable dt = rows.CopyToDataTable<DataRow>();//Copying the rows into the DataTable as DataRow
}
like image 28
Nayas Subramanian Avatar answered Oct 08 '22 20:10

Nayas Subramanian


To Retrieve the DataTable based on filtering the list of item.(i.e.,if any of the list item is present in datatable, that matched result will received.

 List<string>item=new List<string>(){"TG1","TG2"};     
 DataTable tbsplit = (from a in tbl.AsEnumerable()
              where item.Any(x => a.Field<string>("CSubset").ToUpper().Contains(x.ToUpper()))
              select a).CopyToDataTable();//By Executing this, the Filter DataTable is obtained
like image 24
Maghalakshmi Saravana Avatar answered Oct 08 '22 20:10

Maghalakshmi Saravana