C# for循环中的if语句?

C# for循环中的if语句?,c#,if-statement,for-loop,C#,If Statement,For Loop,我有sql背景,正在学习c。现在我有一个for循环,它在x.ph.company、x.ph.product、x.ph.productID等列上运行(所有的标签都是分隔的) 我想在x.product上运行一个if语句,效果是“如果x.product包含“未指定的产品”,然后返回“未指定的技术”。这是我到目前为止得到的,但我不能完全正确。感谢您的帮助 String OutputCustomer; foreach (var x in rs_product_hit) { OutputCustomer =

我有sql背景,正在学习c。现在我有一个for循环,它在x.ph.company、x.ph.product、x.ph.productID等列上运行(所有的标签都是分隔的)

我想在x.product上运行一个if语句,效果是“如果x.product包含“未指定的产品”,然后返回“未指定的技术”。这是我到目前为止得到的,但我不能完全正确。感谢您的帮助

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

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

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

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

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

把它移到局部变量。你看过c#中的.Contains()函数了吗?你能给出你所有的代码吗?如果x.product不包含“Unspecified product”,你想返回什么"? 对于这两种情况,是否仍要返回以制表符分隔的字符串?请注意,它位于
For
循环中这一事实与此无关。这是事实,它;s位于另一条语句(
String.Format
)中,该语句阻止您使用
if
。请将其移到局部变量中。是否查看了c#中的.Contains()函数?你能给出你所有的代码吗?如果x.product不包含“未指定的产品”,你想返回什么?对于这两种情况,是否仍要返回以制表符分隔的字符串?请注意,它位于
For
循环中这一事实与此无关。这是事实,它;另一条语句(
String.Format
)中的,该语句阻止您使用
if