C# 在数据表中查找一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12656526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:01:51  来源:igfitidea点击:

Find a row in a DataTable

c#winforms

提问by Spoon Yukina

I've a table in a DataSet and I want to search for a row in this Table using a unique key.

我在 DataSet 中有一个表,我想使用唯一键在该表中搜索一行。

My question is : Is there any method that allows me to find this row without using loops ?

我的问题是:有没有什么方法可以让我在不使用循环的情况下找到这一行?

This is the code I wrote using the forech loop :

这是我使用 forech 循环编写的代码:

foreach (var myRow in myClass.ds.Tables["Editeur"].AsEnumerable())
{
     if (newKeyWordAEditeurName == myRow[1] as String)
         id_Editeur_Editeur = (int)myRow[0];
}

采纳答案by David W

Sure. You have the Select method off of a DataTable. GEt the table from your DataSet, and use Select to snag it.

当然。您拥有 DataTable 的 Select 方法。从您的数据集中获取表,并使用 Select 来获取它。

void Demo(DataSet ds)
{
    DataTable dt = ds.Tables[0]; // refer to your table of interest within the DataSet

    dt.Select("Field = 1"); // replace with your criteria as appropriate
}