简体   繁体   English

检查DataTable中是否存在字符串/记录

[英]Check if String / Record exists in DataTable

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 DataTable contains the record 但我想知道是否有办法检查DataTable包含记录

Something like this 像这样的东西

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

Use the Find method if item_manuf_id is a primary key: 如果item_manuf_id是主键,请使用Find方法:

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

If you only want to know if the value is in there then use the Contains method. 如果您只想知道值是否在那里,那么使用Contains方法。

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

Primary key restriction applies to Contains aswell. 主键限制适用于Contains以及。

You can loop over each row of the DataTable and check the value. 您可以循环遍历DataTable每一行并检查该值。

I'm a big fan of using a foreach loop when using IEnumerable s. 在使用IEnumerable时,我非常喜欢使用foreach循环。 Makes it very simple and clean to look at or process each row 查看或处理每一行非常简单和干净

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 PrimaryKey for your DataTable . 或者,您可以为您的DataTable使用PrimaryKey This helps in various ways, but you often need to define one before you can use it. 这有助于各种方式,但您通常需要先定义一个,然后才能使用它。

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使用一个示例

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 DataTable also found at at http://msdn.microsoft.com/en-us/library/y06xa2h1(v=vs.80).aspx . 最后,您可以使用Select()方法在DataTable查找数据,该DataTable也位于http://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
}

I think that if your "item_manuf_id" is the primary key of the DataTable you could use the Find method ... 我认为如果你的“item_manuf_id”是DataTable的主键,你可以使用Find方法......

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM