How to filter value using query instead of filter data table

Hi, you can use linq for this

yourDt = yourDt.AsEnumerable.Where(Function(x) x.Item("Index").ToString.StartsWith("0")).CopyToDataTable

But, before make assignment, make sure that the DT has least one row with your condition or it will be throw a exception in CopyDataTable method.

put in if condition

yourDt.AsEnumerable.Where(Function(x) x.Item("Index").ToString.StartsWith("0")).Any

Or you can directly put in assign using if

your dt = if(yourDt.AsEnumerable.Where(Function(x) x.Item("Index").ToString.StartsWith("0")).Any, yourDt.AsEnumerable.Where(Function(x) x.Item("Index").ToString.StartsWith("0")).CopyToDataTable, Nothing)

And check later if yourDt is not Nothing

2 Likes