106

I have DataTable with two columns Author and Bookname.

I want to check if the given string value Author already exists in the DataTable. Is there some built in method to check it, like for Arrays array.contains?

valterriann
  • 1,271
  • 2
  • 10
  • 14

5 Answers5

236

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 do not know the columns Headers and we want to find if any cell value PEPSI exist in any rows'c columns? I can loop it all to 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
  • 450,073
  • 74
  • 686
  • 939
  • Add these `System.Data.DataSetExtensions` to reference and `using System.Linq;` to using class –  Aug 30 '17 at 18:13
  • Out of the two solutions, which would be faster? – Paul Alexander Aug 29 '18 at 11:15
  • 2
    @PaulAlexander: there isn't a great difference. But the old `DataTable.Select` syntax is limited whereas LINQ can use the full .NET framework or custom methods. So only if you are stuck to .NET 2 you should use `DataTable.Select`, otherwise i'd always prefer LINQ – Tim Schmelter Aug 29 '18 at 11:26
  • 1
    If you care about performance and have large datasets, `tbl.Select()` is dramatically faster than the other approaches. – HerrimanCoder Dec 03 '18 at 18:39
  • @TimSchmelter - Great Tim. But if user don't know the column name but still want to get all rows which is matching the search value how it can be done ? – Chandan Kumar Aug 13 '20 at 19:12
  • @ChandanKumar use my second query where i use columns. But instead of Any use Where. – Tim Schmelter Aug 13 '20 at 19:16
17

You can use Linq. Something like:

bool exists = dt.AsEnumerable().Where(c => c.Field<string>("Author").Equals("your lookup value")).Count() > 0;
mservidio
  • 12,817
  • 9
  • 58
  • 84
11
DataRow rw = table.AsEnumerable().FirstOrDefault(tt => tt.Field<string>("Author") == "Name");
if (rw != null)
{
// row exists
}

add to your using clause :

using System.Linq;

and add :

System.Data.DataSetExtensions

to references.

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
5

You should be able to use the DataTable.Select() method. You can us it like this.

if(myDataTable.Select("Author = '" + AuthorName.Replace("'","''") + '").Length > 0)
    ...

The Select() funciton returns an array of DataRows for the results matching the where statement.

Kibbee
  • 65,369
  • 27
  • 142
  • 182
0

you could set the database as IEnumberable and use linq to check if the values exist. check out this link

LINQ Query on Datatable to check if record exists

the example given is

var dataRowQuery= myDataTable.AsEnumerable().Where(row => ...

you could supplement where with any

Community
  • 1
  • 1
Blast_dan
  • 1,135
  • 9
  • 18