Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Get a Specific Column Value from a DataTable?

Tags:

c#

.net

winforms

I have a datatable. I need to fetch a certain column value based on the user input. For example, lets say the datatable has two columns CountryID and CountryName.

I need to find CountryID in the datatable based on the user input country name. I could just open a connection with DB and run the query select countryID from Country where countryName = @userinput. Is there anyway i could do this on the datatable.

like image 487
peace Avatar asked May 26 '10 20:05

peace


1 Answers

string countryName = "USA"; DataTable dt = new DataTable(); int id = (from DataRow dr in dt.Rows               where (string)dr["CountryName"] == countryName               select (int)dr["id"]).FirstOrDefault(); 
like image 172
Seattle Leonard Avatar answered Sep 23 '22 23:09

Seattle Leonard