11

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

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

Andrew
  • 7,619
  • 13
  • 63
  • 117
  • At the risk of sounding redundant, I'm assuming the OP is looking for a solution ensuring **constant** time complexity instead of **linear**, which seems to be what the accepted answer is providing. – Fabien Teulieres Jun 13 '21 at 07:16

4 Answers4

19

Something like this

 string find = "item_manuf_id = 'some value'";
 DataRow[] foundRows = table.Select(find);
Orn Kristjansson
  • 3,435
  • 4
  • 26
  • 40
  • Great solution. As a side note, allow me to suggest a generic query format using brackets, in case your column name contains spaces (even though this does not directly apply to the OP's question): `string find = string.Format("[{0}] = '{1}'", columnName, valueToFind);` – Fabien Teulieres Jun 13 '21 at 07:05
5

Use the Find method if item_manuf_id is a primary key:

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

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

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

Primary key restriction applies to Contains aswell.

Sani Huttunen
  • 23,620
  • 6
  • 72
  • 79
4

You can loop over each row of the DataTable and check the value.

I'm a big fan of using a foreach loop when using IEnumerables. 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. 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

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.

Take a look at 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.

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
}
Kirk
  • 16,182
  • 20
  • 80
  • 112
2

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

string s = "stringValue";
DataRow foundRow = dtPs.Rows.Find(s);
if(foundRow != null) {
 //You have it ...
 }
aleroot
  • 71,077
  • 30
  • 176
  • 213