Linq

How to delete empty rows and rows containing particular character in datatable using a single Linq command?

Hi @shreesha.bhat4 ,

This solves the first half of your query

Dt.AsEnumerable.Where(Function(w) Not(IsNothing(w(0)) OrElse String.IsNullOrEmpty(w(0).ToString))).CopyToDataTable()

For the second half, could you tell us whether this character is present in any of the columns, or in just few given columns or the entire dataset?

Lets say the character is “*” and can be present anywhere in the dataset, then this will work:

Dt.AsEnumerable.Where(Function(w) Not(IsNothing(w(0)) OrElse String.IsNullOrEmpty(w(0).ToString) OrElse w.ItemArray.Contains("*"))).CopyToDataTable()

Kind Regards,
Ashwin A.K

HI @shreesha.bhat4

For deleting empty rows in datatable refer this link

Regards
Sudharsan

Hi @shreesha.bhat4

Try this expression

DtMaster.Rows.Cast(Of DataRow)().Where(Function(row) Not row.ItemArray.All(Function(field) field Is DBNull.Value Or field.Equals(""))).CopyToDataTable()

Regards
Gokul

we assume check for all cols

here is unclear if all or a particular column is to check

Empty All cols, All cols check for char:

(From d in dtData.AsEnumerable
Where Not d.ItemArray.Any(Function (x) isNothing(x) OrElse String.IsNullorEmpty(x.toString.Trim))
Where Not d.ItemArray.Any(Function (x) x.toString.Contains("YourCheckString")
Select r=d).CopyToDataTable

Empty All cols, single cols check for char:

(From d in dtData.AsEnumerable
Where Not d.ItemArray.Any(Function (x) isNothing(x) OrElse String.IsNullorEmpty(x.toString.Trim))
Where Not dYourColNameOrIndex).toString.Contains("YourCheckString")
Select r=d).CopyToDataTable
1 Like

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