Find row in datatable with specific id

前端 未结 8 1770
醉酒成梦
醉酒成梦 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:48

    Make a string criteria to search for, like this:

    string searchExpression = "ID = 5"
    

    Then use the .Select() method of the DataTable object, like this:

    DataRow[] foundRows = YourDataTable.Select(searchExpression);
    

    Now you can loop through the results, like this:

    int numberOfCalls;
    bool result;
    foreach(DataRow dr in foundRows)
    {
        // Get value of Calls here
        result = Int32.TryParse(dr["Calls"], out numberOfCalls);
    
        // Optionally, you can check the result of the attempted try parse here
        // and do something if you wish
        if(result)
        {
            // Try parse to 32-bit integer worked
    
        }
        else
        {
            // Try parse to 32-bit integer failed
    
        }
    }
    

提交回复
热议问题