您好,如何使用linq to datatable过滤datatable?我有一个DropDownList,在那里我可以选择模块列的值。现在我想用这个模块列来过滤DataTable。
下面是我的datatable结构:
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);
}发布于 2013-10-18 20:31:12
您最好使用DataTable.Select方法,但是如果您必须使用LINQ,那么您可以尝试:
DataTable selectedTable = tb.AsEnumerable()
.Where(r => r.Field<string>("Modul") == value)
.CopyToDataTable();这将基于筛选的值创建一个新的DataTable。
如果您使用DataTable.Select
string expression = "Modul =" + value;
DataRow[] selectedRows = tb.Select(expression);发布于 2017-06-14 16:16:47
在强制转换之前,您可以使用condition来检查行是否存在。Any()需要System.Linq命名空间才能工作
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
}发布于 2020-01-23 20:28:54
根据过滤条目的列表来检索datatable。(即,如果数据表中存在任何列表项,则将接收匹配的结果。
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 obtainedhttps://stackoverflow.com/questions/19449449
复制相似问题