How to pass variable for the column name in Select method of data table?

Hi All

In select method i want to pass variable for the column name. The column name may contain spaces. How to do that.

Passing variable for coulmn name as it will interact with multiple excels and reuse the same code.
currQtrDT.Select("[currQtrDT.Columns(0).ToString]="+"’"+row(0).ToString+"’") throwing error.

Hi @kkpatel

Please try this syntax

dtTable.Select("["+dtTable.Columns(0).ToString+"] = ‘"+CurrentRow(“name”).ToString+"’")

Syntax error: Missing operand after ‘21’ operator.

Getting above error.

select method

Its because of the single quote present in the data i guess.

you can replace the single quote and try.

Hi @kkpatel , @prasath_S

currQtrDT.AsEnumerable.Select(Function(f1) f1.Field(Of String)(“Month Year”) = CurrentRow(“name”).ToString)

allows you to use column names with spaces.

I don’t want to hard code the column name. I want to pass variable. Also the above method I am not able to understand properly.

Hi @kkpatel

You can passed a variable this way

currQtrDT.AsEnumerable.Select(Function(f1) f1. Field(Of String)(str_varField) == CurrentRow(“name”).ToString)

where str_varField is a variable type of String

See @ppr LINQ learning materials for better understanding of LINQ syntax.

1 Like