Linq Expression-Error

Hi
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()

Cheers
@KarthikBallary

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

2 Likes

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

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

1 Like