Akjell
Akjell

Reputation: 109

Look for a value in a DataTable column

I have a DataTable dt that have a Column month it looks like this.

month
yes
yes

I want to check if the column month contains "yes". I don't have a primary key in the Datatable dt. Something like this

if( dt.["month"] == "yes")
 boolMonth = true;

Upvotes: 0

Views: 60

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98740

Also you can use LINQ to DataSet like (assume month type is string);

bool boolMonth  = dt.AsEnumerable().
                     Any(row => row.Field<string>("month") == "yes");

Upvotes: 1

fubo
fubo

Reputation: 45947

assumed, you want to check if any row equals the string value "yes":

if(dt.Rows.Cast<DataRow>().Any( x => (string)x["month"] == "yes"))
boolMonth = true;

Upvotes: 1

Related Questions