How to Get a Specific Column Value from a DataTable?

前端 未结 6 1933
没有蜡笔的小新
没有蜡笔的小新 2020-12-13 02:08

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

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 02:35

    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();
    

提交回复
热议问题