How to use an IF statement to filter values in a DataSet column?

Hi, I'm new to C# and I'm currently stuck at using an IF statement to filter certain values within a dataset. A DataView isn't an option since I'm trying to loop through the entire record set.

Specifically, I'm trying see if the object "Type" equals to "Outage" or "TR Outage". I'm getting compile time errors saying that it cannot implicitly convert object to bool and another one that cannot use operand ||.

Here's the code I have (problem highlight in bold). I appreciate any help I can get.

bool theFlag = false;
foreach (DataRow TheDataRow in TheData.Tables["outageRecord"].Rows)
{
if ( (bool)TheDataRow["type"] = "Outage" || TheDataRow["type"] = "TR Outage")
{
if ( Convert.ToInt32( TheDataRow["outCurrentCustomersEffected"] ) > 1 )
{
theFlag = true;
break;
}
}
}


Answer this question

How to use an IF statement to filter values in a DataSet column?

  • DanInNewWest

    Thanks for your help Maheswari. Your code solved the problem I had.

  • Eric H.

    The following code segment will work.

    bool theFlag = false;
    foreach (DataRow TheDataRow in TheData.Tables["outageRecord"].Rows)
    {
    if ( (TheDataRow["type"].ToString() == "Outage") || (TheDataRow["type"].ToString() == "TR Outage") )
    {
    if ( Convert.ToInt32( TheDataRow["outCurrentCustomersEffected"].ToString() ) > 1 )
    {
    theFlag = true;
    break;
    }
    }
    }

    the problem is, single equals(=).

    = is used to assign a values

    == is used to compare two values.

    in if statemets we are going to compare two values so we needs to use == (double equals)

    and also objects cannot be compared directly to strings. so we need to convert that object to string by using ToString() method.



  • How to use an IF statement to filter values in a DataSet column?