Linq query to filter datatable with null values

Hello Community!

I want to filter a datatable with null values. I would prefer to use a linq query as the data table would have lots of rows.

The datatable looks something like this:
image
The column “Type” has dynamic values.

I only want to get the rows with null/no values in the “Type” column.
This is my linq query so far:

(From r In myDT.AsEnumerable
Where r("Type").toString.Contains("")
Select r).CopyToDataTable

My problem is, it doesn’t filter at all when i use Contains(“”), it just gets the whole datatable
but if I try Contains(“new1”), it works fine and filters all the rows with the “new1” value in the “Type” column
So I’m not sure how to filter the null values in the right way. Any help would be greatly appreciated.

Thanks in Advance!

1 Like

Hi @mimuhrin try this

dt1.AsEnumerable().Where(Function (row) row(“Type”).ToString.Equals(" ")). CopyToDataTable

Regards

Nived N :robot:

Happy Automation :relaxed::relaxed::relaxed::relaxed:

1 Like

Use this below linq to get null rows, @mimuhrin
TestDt.AsEnumerable.Where(Function(x) String.IsNullOrEmpty(x(“Type”).ToString.Trim)).CopyToDataTable()

Hope this may help you :slight_smile:

2 Likes

I believe you’re doing something wrong, because I applied exact same logic and it worked.
I suggest you to follow two steps:

  1. Check preserve format checkbox while reading excel.
  2. Create new variable of datatable type and assign same value to it.

Try it out.

This works for me as well :slight_smile:
a follow up question, what if I want to filter all null/empty string AND all “new1” values?

You can use this query :slight_smile:
TestDt.AsEnumerable.Where(Function(x) String.IsNullOrEmpty(x(“Type”).ToString.Trim) And x(“Type”).ToString.Trim.Equals(“new1”)).CopyToDataTable()

2 Likes

perfect! thanks!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.