Find row in datatable with specific id

前端 未结 8 1771
醉酒成梦
醉酒成梦 2020-12-05 00:07

I have two columns in a datatable:

ID, Calls. 

How do I find what the value of Calls is where ID = 5?

5 could be anynu

8条回答
  •  渐次进展
    2020-12-05 00:43

    You can use LINQ to DataSet/DataTable

    var rows = dt.AsEnumerable()
                   .Where(r=> r.Field("ID") == 5);
    

    Since each row has a unique ID, you should use Single/SingleOrDefault which would throw exception if you get multiple records back.

    DataRow dr = dt.AsEnumerable()
                   .SingleOrDefault(r=> r.Field("ID") == 5);
    

    (Substitute int for the type of your ID field)

提交回复
热议问题