Remove rows with no data from datatable

Hello,

I am looping through data and populating a datatable.
However, before writing to Excel, would like to remove rows that have no matches.

image
In example above, grape and pear would be removed, as they had no matches.
How can i do this?
Regards

We can do:

Assign Activity
dtCleansed =

(From d in YourDataTableVar.AsEnumerable
Let chk = d.ItemArray.Skip(1).All(Function (x) isNothing(x) OrElse String.IsNullorEmpty(x.toString.Trim))
Where not chk
Select r = d).CopyToDataTable

Handling emtpy results we do:

1 Like

Thank you so much.
Tested and works great.
Greatly reduces output.
Appreciate the help.

Hi PPR,

Similar question. Will it be a new search to remove the empty columns as well.
Like Thursday had no match.
Regards,

removing rows where any col is a blank:

(From d in YourDataTableVar.AsEnumerable
Let chk = d.ItemArray.Skip(1).Any(Function (x) isNothing(x) OrElse String.IsNullorEmpty(x.toString.Trim))
Where not chk
Select r = d).CopyToDataTable

That is the same formula.
Sorry for not being clearer.
Is it possible to remove columns as well that have no matching data?
Like Thursday above?
Regards

No is not the same on one we are using All on the other we are using Any.

We recommend to use the reference information and have a closer look on LINQ and its operators

Thank you. The remove rows worked well. However, removing empty columns results in “value cannot be null” error.

we prefer to have scoped 1 topic = 1 use case
just open a new topic for your second question and share with us

  • sample input example data
  • expected output sample
    Thanks

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