在数据表中查找具有特定 id 的行 [英] Find row in datatable with specific id

查看:23
本文介绍了在数据表中查找具有特定 id 的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据表中有两列:

ID, Calls. 

如何找到 Calls 的值 where ID = 5?

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

5 可以是任意数字,仅作为示例.每行都有一个唯一的 ID.

5 could be anynumber, its just for example. Each row has a unique ID.

推荐答案

制作一个字符串搜索条件,像这样:

Make a string criteria to search for, like this:

string searchExpression = "ID = 5"

然后使用DataTable对象的.Select()方法,像这样:

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

    }
}

这篇关于在数据表中查找具有特定 id 的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆