Filter Data Table To Get Only Even Numbers

Hi All,

i have a datatable name XYZ .Having columns # , Points.

containing data like 1,2,3,4,5,6 … 55.total 55 rows.

i want to filter the above datatable with coulmn # to get only even rows.

i tried using select but didnt worked.

Can anyone please help me on this ?

Thanks

hope this expression would help you resolve this
yourdatatablename = yourdatatablename.AsEnumerable().Where(Function(a) Convert.ToInt32(a.Field(Of String)(“youcolumnname”)) mod 2 = 0).CopyToDatatable()

Cheers @avinashy

2 Likes

@Palaniyappan : Thanks for your quick response.

i am not getting any method with “Field” writing the query.

And my column name is "# ",do you want me to replace "a " with # in the above expression ?

Fine
along with the expression
we can just mention like this
yourdatatablename = yourdatatablename.AsEnumerable().Where(Function(a) Convert.ToInt32(a.Field(Of String)(“#”)) mod 2 = 0).CopyToDatatable()

@avinashy

@Palaniyappan : It worked thank you.

May i know why it didnt worked with select query ?

1 Like

Fantastic
can i have a view on the select query been used
Cheers @avinashy

@Palaniyappan : i used the select query which you posted above one only . i think it replaced with where.

1 Like

aah sorry that was a typing mistake :wink:
copytodatatable is not a member of asenumerable with select method with a boolean condition
so we can use only where method here @avinashy
thats why select didnt work
Cheers

@Palaniyappan : No problem i have another query,please update.

i got the sum of values from xyz column in datatble and the sum is 3456.

Now i want to get the name from corresponding column ABC closer or equal to this above sum 3456.

EX: Datatble and columns and values as exists below

ABC XYZ

NAME 3456

Thanks

same
yourdatatablename = yourdatatablename.AsEnumerable().Where(Function(a) Convert.ToInt32(a.Field(Of String)(“ABC”)) >= 3456).CopyToDatatable()

Cheers @avinashy

@Palaniyappan : in the place 3456 i want to pass variable.

And the corresponding value which i am trying to fetch is string so i think no need to convert to int32 right ?
can u modify ?

Fine
yourdatatablename = yourdatatablename.AsEnumerable().Where(Function(a) Convert.ToInt32(a.Field(Of String)(“ABC”)) >= Convert.ToInt32(yourvariablename)).CopyToDatatable()

Cheers @avinashy