13 July 2011

Linq on DataTable

Hello everybody,

Sometimes we need to easily make a select on a table, and usually most of developers will use loop through the rows of the DataTable and find the row that developer is searching for or the collection of rows.

I came across this problem too, and I was thinking why it isn't possible to use Linq to select those rows?


To solve this we need to make from the DataTable an Enumerable. Below I have provided two examples in linq - sql style and in linq using expressions.

Code:
myTable.AsEnumerable()
    .Where(r => r.Field<int>("automatic_action_required") == 1)

Code:
from myRow in myTable.AsEnumerable()
where myRow.Field<int>("automatic_action_required") == 1
select myRow;

Which one to use is already your choice.

Thanks for attention.

No comments:

Post a Comment

your thoughts are welcome:

Need more? Leave comments and subscribe to my blog.

.