Linq Expression-Error

I need to fetch data from datable where gender is Male.

is this linq query correct?
(from g in dt_gender where g[“Gender”].toString() Equals “M” Select g)

I am getting Identifier is expected.

(from g in dt_gender.AsEnumerable() where g[“Gender”].toString.Equals(“M”) Select g(“yourColumnName”)).ToList()


Not working, same error

1 Like

can you share the error screenshot

I need o/p as dt

1 Like

(From row in DataTable.AsEnumerable() where row(“Gender”).ToString.Equals(“M”) Select row(“Gender”)).ToList()

just copy paste the above LINQ and try

Thanks! this works fine. but what if I need to fetch multiple columns and I need this as DT. Copy toDatatable()

1 Like

for that you can try this

dataTable.AsEnumerable.Where( Function(x) x(“Gender”).Equals(“M”)).CopyToDataTable


dataTable.AsEnumerable.Where( Function(x) x(“Gender”).ToString.Equals(“M”)).CopyToDataTable

Please note, this expression will throw an error if your datatable does not have any rows where gender is male at all.

1 Like