如何使用 Linq 过滤数据表到数据表? [英] How I can filter a dataTable with Linq to datatable?

查看:31
本文介绍了如何使用 Linq 过滤数据表到数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用 linq to datatable 过滤数据表?我有一个 DropDownList,在那里我可以选择 Modul Column 的值.现在我想用这个模块列过滤数据表.

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.

这是我的数据表结构:

User | Host | TimeDiff | License | Telefon | Modul 

代码如下:

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);
}

推荐答案

你最好使用 DataTable.Select 方法,但如果你必须使用 LINQ 那么你可以尝试:

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();

这将基于过滤的值创建一个新的 DataTable.

This would create a new DataTable based on filtered values.

如果您使用 DataTable.Select

string expression = "Modul =" + value;
DataRow[] selectedRows = tb.Select(expression);

这篇关于如何使用 Linq 过滤数据表到数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆