if循环中的if语句?

时间:2016-03-16 20:05:47

标签: c# if-statement for-loop

我有一个sql背景,我正在学习c#。现在我有一个for循环,可以在列x.ph.company,x.ph.product,x.ph.productID等上运行(所有制表符分隔)

我想在x.product上运行一个if语句,其效果为“If x.product contains”Unspecified Product“,然后返回”Unspecified Tech“。这是我到目前为止所得到的但是我可以不太对劲。任何帮助表示赞赏!

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

1 个答案:

答案 0 :(得分:5)

你可以使用三元语法吗?还有很多其他方法,但这似乎与你已经做过的事情最接近我。

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