Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Query on Datatable to check if record exists

Tags:

c#

linq

I want to perform a LINQ query on a datatable called Records and check if a record exists. If it exists, I want to find out the row which it is in. How might I go about doing this?

I wanted to do a .where on my datatable after adding the system.linq namespace but the method didnt seem to exist. Please advise

P.S : Am using c# in vs 2010

like image 571
paradox Avatar asked Feb 24 '23 03:02

paradox


1 Answers

DataTable is not default uses Enumerable. you have to convert to

  var result = from p in dataTable.AsEnumerable()
     where p.Field("ID") == 2
    select p.Field("Name");

   if(result.Any())
   {
      //do your work
    }

read this article for

http://blogs.msdn.com/b/adonet/archive/2007/01/26/querying-datasets-introduction-to-linq-to-dataset.aspx

getting understanding your to use Field<T>

like image 153
anishMarokey Avatar answered Mar 07 '23 23:03

anishMarokey