Deleting Empty Rows Using LINQ Cast Function

Hello Everyone,

I am trying to delete empty rows from DataTable - “DT” initialized as shown in below screenshot,

I have written code as below using LINQ Cast function,

(From row In DT
From col In DT.Columns.Cast(Of DataColumn)
Where Not String.IsNullOrEmpty(row(col).ToString)
Select row
).CopyToDataTable

But, this giving me error as,

Am i missing anything?

Hi @NILESH.BOT369

Can you try the below syntax:

(From row In DT.AsEnumerable()
 From col In DT.Columns.Cast(Of DataColumn)()
 Where Not String.IsNullOrEmpty(DirectCast(row(col), String))
 Select row
).CopyToDataTable()

Regards

Hi @vrdabberu , thank you for your response,
Its still giving the error,

@NILESH.BOT369

  1. Easiest would be filter datatable
  2. Here is linq dt.AsEnumerable.Where(function(x) Not IsNothing(x(0)) AndAlso Not String.IsNullOrEmpty(x(0).ToString)).CopyToDataTable

Cheers

1 Like

Hi @NILESH.BOT369

Can you try the below syntax:

DT.AsEnumerable().Where(Function(row) Not row.ItemArray.All(Function(cell) cell Is DBNull.Value OrElse cell.Equals(""))).CopyToDataTable()

Input:


Output:

Regards

1 Like

Great, this works Perfect!, Appreciate, thank you.

1 Like

You’re welcome @NILESH.BOT369

Happy Automation!!

1 Like

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