我如何过滤与LINQ的一个DataTable到数据表? [英] How I can filter a dataTable with Linq to datatable?

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

问题描述

您好,我怎么可以过滤的LINQ一个DataTable到数据表?
我有一个DropDownList在那里我可以选择MODUL列的值。现在我想这个MODUL列来过滤数据表。

这是我的数据表结构:

 用户|主机| TIMEDIFF |许可证|电话| MODUL

在这里,code:

 保护无效drp_Modules_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
    字符串值= drp_Modules.SelectedValue;    数据表TB =(数据表)会议[dt_Users];    TB =从项目结核病// ?????    LoadUsertable(TB);
}


解决方案

您使用的是更好的<一个href=\"http://msdn.microsoft.com/en-us/library/system.data.datatable.select.aspx\"><$c$c>DataTable.Select方法,但是如果你要使用LINQ,那么你可以尝试一下:

 的DataTable selectedTable = tb.AsEnumerable()
                            。凡(R = GT; r.Field&LT;串GT(MODUL)==值)
                            .CopyToDataTable();

这将创建一个新的数据表基础上筛选值。

如果您使用 DataTable.Select

 字符串前pression =MODUL =+值;
的DataRow [] = selectedRows tb.Select(如pression);

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

解决方案

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

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

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