Linq query to filter data from datatable

how to write linq query to filter out from compare column which has values of $0.00 and $0.01 from the data table

i want data excluding $0.00 and $0.01

Hi @T_Y_Raju

Try this

filteredDataTable = dataTable.AsEnumerable().Where(Function(row) Not (row.Field(Of String)("CompareColumn") = "$0.00" Or row.Field(Of String)("CompareColumn") = "$0.01")).CopyToDataTable()

Regards,

of filed should string or double because the values are like $.0.00,$0.01,$12,345.78

@T_Y_Raju

Since the values include currency formatting ($0.00, $0.01, $12,345.78), they should be treated as strings when filtering.

Regards,

but iam getting error as unable to cast object of type system.Decimal to type system.String

getting error as option strict disallows operands of type Object for operator =

@T_Y_Raju

Try the below syntax

DT.AsEnumerable().Where(Function(row) Not (row("Taxes").ToString.Trim.Equals("0") or row("Taxes").ToString.Trim.Contains("0.00") Or row("Taxes").ToString.Trim.Contains("0.01"))).CopyToDataTable()

its not filtering the data

InputFile.xlsx (8.2 KB)
i have attached input file plz remove $0.00 and $0.01 data

Hi @T_Y_Raju ,

Try below expression

Input_DT.AsEnumerable().Where(Function(row) Not (row("Compare").ToString() = "$0.00" Or row("Compare").ToString() = "$0.01")).CopyToDataTable()

image

image

Hope it helps you

Regards,
Vinit Mhatre

1 Like

@T_Y_Raju

How about the following?

Code:

Dim rowsToDelete As New List(Of DataRow)

For Each row As DataRow In dtInput.Rows
    Dim compareValue As String = row("Compare").ToString().Trim()
    If compareValue = "$0.00" Or compareValue = "$0.01" Then
        rowsToDelete.Add(row)
    End If
Next

For Each row As DataRow In rowsToDelete
    dtInput.Rows.Remove(row)
Next

Output:

image

Cheers!!

Hi @T_Y_Raju

Try this Linq query

ReadDT.AsEnumerable().Where(Function(row) Not (row(“Price”).ToString() = “$0.00” Or row(“Price”).ToString() = “$0.01”)).CopyToDataTable()

Output

Hope it works.
Cheers

1 Like