How I can filter a dataTable with Linq to datatable?

前端 未结 3 1552
执念已碎
执念已碎 2020-12-14 02:59

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

相关标签:
3条回答
  • 2020-12-14 03:02

    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
    
    0 讨论(0)
  • 2020-12-14 03:13

    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
    }
    
    0 讨论(0)
  • 2020-12-14 03:14

    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);
    
    0 讨论(0)
提交回复
热议问题