Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we filter Datatable with LINQ?

Tags:

linq

Suppose my datatable is filled with data. After filling data can we again put some condition on datatable with linq to extract data.

Suppose my datatable has 10 employee record. So can we extract only those employee whose salary is greater than 5000 with linq query. I know that we can achieve it datatable.select(). How can you achieve this with linq?

like image 836
Thomas Avatar asked Apr 25 '11 06:04

Thomas


2 Answers

You can get a filtered set of rows, yes:

var query = table.AsEnumerable()
                 .Where(row => row.Field<decimal>("salary") > 5000m);

This uses the AsEnumerable and Field extension methods in DataTableExtensions and DataRowExtensions respectively.

like image 192
Jon Skeet Avatar answered Nov 06 '22 13:11

Jon Skeet


Try this:

                var query = (from t0 in dtDataTable.AsEnumerable()
                where t0.Field<string>("FieldName") == Filter

               select new
               {
                   FieldName  = t0.Field<string>("FieldName"),
                   FieldName2 = t0.Field<string>("FieldName2"),
                });
like image 21
Yuri Cano Avatar answered Nov 06 '22 13:11

Yuri Cano



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!