重构我的DataTable搜索代码

Jon*_*ury 0 c# search refactoring

我正在使用C#2.0,我不禁认为这不是搜索集合(在本例中为DataTable)获取值的最有效方法:

bool found = false;
foreach (DataRow row in data.Rows)
{
    if (id == row["rowID"])
    {
        found = true;
        break;
    }
}
if (!found)
{
    //Do stuff here
}
Run Code Online (Sandbox Code Playgroud)

谁能想到一种"更清洁"的方式呢?

Joe*_*orn 5

查看数据表的Select()方法:

http://msdn.microsoft.com/en-us/library/b5c0xc84(VS.80).aspx

DataRow[] rows = data.Select("rowID=" + id.ToString());
if (rows.Length > 0)
{
    //Do stuff here
}
Run Code Online (Sandbox Code Playgroud)