简体   繁体   中英

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"
}

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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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