You can use LINQ-to-DataSet with Enumerable.Any :
String author = "John Grisham"; bool contains = tbl.AsEnumerable().Any(row => author == row.Field<String>("Author"));
Another approach is to use DataTable.Select :
DataRow[] foundAuthors = tbl.Select("Author = '" + searchAuthor + "'"); if(foundAuthors.Length != 0) { // do something... }
Q: what if we don't know the Header columns, and we want to find if they PEPSI cell value exists in the columns of any rows? I can find out, but is there a better way? -
Yes, you can use this query:
DataColumn[] columns = tbl.Columns.Cast<DataColumn>().ToArray(); bool anyFieldContainsPepsi = tbl.AsEnumerable() .Any(row => columns.Any(col => row[col].ToString() == "PEPSI"));
Tim Schmelter May 22 '12 at 1:46 pm 2012-05-22 13:46
source share