Tarasov Tarasov -4 years ago 66
ASP.NET (C#) Question

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 //?????


Answer Source

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)

This would create a new DataTable based on filtered values.

If you use DataTable.Select

string expression = "Modul =" + value;
DataRow[] selectedRows = tb.Select(expression);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download