C# 检查数据表中是否存在字符串/记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11961059/
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-09 19:51:16  来源:igfitidea点击:

Check if String / Record exists in DataTable

c#datatable

提问by Andrew

I have a String and I need to check if any column "item_manuf_id" in DataTable dtPs.Rows equals to certain value

我有一个字符串,我需要检查 DataTable dtPs.Rows 中的任何列“item_manuf_id”是否等于某个值

I can loop over all Rows and compare

我可以遍历所有行并进行比较

String id = dtPs.Rows[number]["item_manuf_id"].ToString()
if ("some value".equals(id)) etc.

but I am wondering if there is any way to check if DataTablecontains the record

但我想知道是否有任何方法可以检查是否DataTable包含记录

采纳答案by Orn Kristjansson

Something like this

像这样的东西

 string find = "item_manuf_id = 'some value'";
 DataRow[] foundRows = table.Select(find);

回答by aleroot

I think that if your "item_manuf_id" is the primary key of the DataTable you could use the Findmethod ...

我认为如果您的“item_manuf_id”是 DataTable 的主键,您可以使用Find方法......

string s = "stringValue";
DataRow foundRow = dtPs.Rows.Find(s);
if(foundRow != null) {
 //You have it ...
 }

回答by Sani Singh Huttunen

Use the Findmethod if item_manuf_idis a primary key:

如果是主键,则使用Find方法item_manuf_id

var result = dtPs.Rows.Find("some value");

If you only want to know if the value is in there then use the Containsmethod.

如果您只想知道该值是否在其中,请使用Contains方法。

if (dtPs.Rows.Contains("some value"))
{
  ...
}

Primary key restriction applies to Containsaswell.

主键限制也适用于Contains

回答by Kirk

You can loop over each row of the DataTableand check the value.

您可以遍历 的每一行DataTable并检查值。

I'm a big fan of using a foreachloop when using IEnumerables. Makes it very simple and clean to look at or process each row

我非常喜欢在使用s时使用foreach循环IEnumerable。使查看或处理每一行变得非常简单和干净

DataTable dtPs = // ... initialize your DataTable
foreach (DataRow dr in dtPs.Rows)
{
    if (dr["item_manuf_id"].ToString() == "some value")
    {
        // do your deed
    }
}

Alternatively you can use a PrimaryKeyfor your DataTable. This helps in various ways, but you often need to define one before you can use it.

或者,您可以PrimaryKey为您的DataTable. 这在很多方面都有帮助,但您通常需要先定义一个,然后才能使用它。

An example of using one if at http://msdn.microsoft.com/en-us/library/z24kefs8(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/z24kefs8(v=vs.80).aspx上使用 if 的示例

DataTable workTable = new DataTable("Customers");

// set constraints on the primary key
DataColumn workCol = workTable.Columns.Add("CustID", typeof(Int32));
workCol.AllowDBNull = false;
workCol.Unique = true;

workTable.Columns.Add("CustLName", typeof(String));
workTable.Columns.Add("CustFName", typeof(String));
workTable.Columns.Add("Purchases", typeof(Double));

// set primary key
workTable.PrimaryKey = new DataColumn[] { workTable.Columns["CustID"] };

Once you have a primary key defined and data populated, you can use the Find(...)method to get the rows that match your primary key.

一旦定义了主键并填充了数据,就可以使用Find(...)方法获取与主键匹配的行。

Take a look at http://msdn.microsoft.com/en-us/library/y06xa2h1(v=vs.80).aspx

看看http://msdn.microsoft.com/en-us/library/y06xa2h1(v=vs.80).aspx

DataRow drFound = dtPs.Rows.Find("some value");
if (drFound["item_manuf_id"].ToString() == "some value")
{
    // do your deed
}

Finally, you can use the Select()method to find data within a DataTablealso found at at http://msdn.microsoft.com/en-us/library/y06xa2h1(v=vs.80).aspx.

最后,您可以使用Select()方法DataTablehttp://msdn.microsoft.com/en-us/library/y06xa2h1(v=vs.80).aspx 中找到的数据中查找数据。

String sExpression = "item_manuf_id == 'some value'";
DataRow[] drFound;
drFound = dtPs.Select(sExpression);

foreach (DataRow dr in drFound)
{
    // do you deed. Each record here was already found to match your criteria
}