Ethan E
Ethan E

Reputation: 29

if statement inside a for loop?

I have a sql background and I'm learning c#. Right now I have a for loop that operates on columns x.ph.company, x.ph.product, x.ph.productID, etc (all tab-delimited)

I want to run an if statement on x.product with something to the effect of "If x.product contains "Unspecified Product", then return "Unspecified Tech". This is what I've got so far but I can't quite get it right. Any help appreciated!

String OutputCustomer;
foreach (var x in rs_product_hit)
{
OutputCustomer = String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n"
, x.ph.hit_id
, SetAsSpace(x.ph.url)
, SetAsSpace(x.ph.company)
, SetAsSpace(x.ph.City)
, SetAsSpace(x.ph.State)
, SetAsSpace(x.ph.iso)
, SetAsSpace(x.ph.vendor)
, SetAsSpace(x.ph.product)
if ( x.ph.product == "Unspecified Product" )
{
   x.ph.product = "Unspecified Tech"
}

Upvotes: 0

Views: 133

Answers (1)

Mike Wallace
Mike Wallace

Reputation: 543

You could do it with the Ternary syntax? There are many other ways but this seems closest to what you're already doing, to me.

    String OutputCustomer;
    foreach (var x in rs_product_hit)
    {
       OutputCustomer = String.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\r\n"
                                    , x.ph.hit_id
                                    , SetAsSpace(x.ph.url)
                                    , SetAsSpace(x.ph.company)
                                    , SetAsSpace(x.ph.City)
                                    , SetAsSpace(x.ph.State)
                                    , SetAsSpace(x.ph.iso)
                                    , SetAsSpace(x.ph.vendor)
                                    , SetAsSpace(x.ph.product)
                                    ,x.ph.product == "Unspecified Product" ?
                                     "Unspecified Tech" : x.ph.product);
    }

Upvotes: 5

Related Questions