TimeIsNear
TimeIsNear

Reputation: 755

How to check if int value exist in a dataTable

How can check if the value exist in datatable column (int)

For example, I search if "9" exist in the column named "entryHour".

bool exists = listAgenda.AsEnumerable().Where(c => c.Field<string>("entryHour").Equals(9)).Count() > 0;

Thanks for your help.

Upvotes: 0

Views: 1186

Answers (2)

Oliver
Oliver

Reputation: 36393

It looks like you are mixing between a string and int values. You need to compare apples with apples.

bool exists = listAgenda.AsEnumerable().Any(c => c.Field<string>("entryHour") == "9");

Upvotes: 0

M. Nasir Javaid
M. Nasir Javaid

Reputation: 5990

If you have integer type field in database then try this

bool exists = listAgenda.AsEnumerable().Any(c => c.Field<int>("entryHour") == 9));

But if you have string type then need to cast into integer first

bool exists = listAgenda.AsEnumerable().Any(c => int.Parse(c.Field<string>("entryHour")) == 9));

Upvotes: 3

Related Questions