如何在C#中打印除一个数据行之外的每个数据行

时间:2022-11-05 21:42:17

I have a for loop to print a sets of addresses. But i want to do a condition where it skip printing the set of address that has the same value as postalCode-X.

我有一个for循环来打印一组地址。但我想做一个条件,它跳过打印与postalCode-X具有相同值的地址集。

So how to can i escape from a forloop? the language is in C#,.NET framework. thanks.

那么如何才能摆脱forloop?语言是在C#,. NET框架中。谢谢。

the DATA look like this:

DATA看起来像这样:

SET 1 
Street 1
California 1770 

SET 2 
Street 2 
New Jersey 1990 

SET 3 
Street 3 
Oakland 2000

Output with exception of postalcode 1990:

输出除1990年邮政编码外:

Print SET1 and SET3 only.

仅打印SET1和SET3。

foreach (Dataset.Row dr in A.Rows)
        {
            if (dr.id("1"))
            {
                string unit = dr.unit;
                string streetName = dr.street;
                string postalCode = dr.postal;
                content += String.Format("{0}", dr.name);
                if (showAddress)
                    content += "<br/>" + GenAddr(unit,streetName, postalCode) + "<br/>";
            }
        }

3 个解决方案

#1


2  

//inside the loop
if(!currentset.PostalCode.Equals("1990"))
    {
    Console.WriteLine("Set: "+currentset);
    }

A simple if statement does what you want. I could give you an answer more customized for your situation if you would update the question with your loop and what data type the sets are etc.

一个简单的if语句可以满足您的需求。如果您要使用循环更新问题以及集合的数据类型等,我可以为您提供更适合您情况的答案。

Note that the above code is not "escaping" the loop. It's just selectively printing out sets from inside the for loop. The code below makes use of the continue statement in C# to "escape" the rest of the code in the loop.

请注意,上面的代码不是“转义”循环。它只是选择性地从for循环内部打印出来。下面的代码利用C#中的continue语句来“转义”循环中的其余代码。

//inside the loop
if(currentset.PostalCode.Equals("1990")
{
    continue; //the curly braces are unnecessary for a single line in the if statement
}
Console.WriteLine("Set: "+currentset); //notice this is after the continue statement

Another option would be to have List and then remove the sets with Linq that you don't want prior to having a loop that prints out the sets.

另一种选择是使用List,然后在具有打印出集合的循环之前删除Linq中您不想要的集合。

List<set> sets=GetSets();
set.RemoveAll(aset => aset.PostalCode.Equals("1990"));
//now loop through sets
foreach(set currentset in sets)
{
    Console.WriteLine("Set: "+set);
}

#2


1  

Or using linq.

或者使用linq。

yourList.Where(x=>!x.PostalCode.Equals("1990")).ForEach(Console.WriteLine)

#3


0  

Also you have another way to do:

你还有另一种方法:

//inside the loop
if(!currentset.PostalCode.Equals("1990"))
{
    Console.WriteLine("Set: "+currentset);
}

OR

要么

//inside the loop
if(currentset.PostalCode.Equals("1990"))
    continue;
Console.WriteLine("Set: "+currentset);

#1


2  

//inside the loop
if(!currentset.PostalCode.Equals("1990"))
    {
    Console.WriteLine("Set: "+currentset);
    }

A simple if statement does what you want. I could give you an answer more customized for your situation if you would update the question with your loop and what data type the sets are etc.

一个简单的if语句可以满足您的需求。如果您要使用循环更新问题以及集合的数据类型等,我可以为您提供更适合您情况的答案。

Note that the above code is not "escaping" the loop. It's just selectively printing out sets from inside the for loop. The code below makes use of the continue statement in C# to "escape" the rest of the code in the loop.

请注意,上面的代码不是“转义”循环。它只是选择性地从for循环内部打印出来。下面的代码利用C#中的continue语句来“转义”循环中的其余代码。

//inside the loop
if(currentset.PostalCode.Equals("1990")
{
    continue; //the curly braces are unnecessary for a single line in the if statement
}
Console.WriteLine("Set: "+currentset); //notice this is after the continue statement

Another option would be to have List and then remove the sets with Linq that you don't want prior to having a loop that prints out the sets.

另一种选择是使用List,然后在具有打印出集合的循环之前删除Linq中您不想要的集合。

List<set> sets=GetSets();
set.RemoveAll(aset => aset.PostalCode.Equals("1990"));
//now loop through sets
foreach(set currentset in sets)
{
    Console.WriteLine("Set: "+set);
}

#2


1  

Or using linq.

或者使用linq。

yourList.Where(x=>!x.PostalCode.Equals("1990")).ForEach(Console.WriteLine)

#3


0  

Also you have another way to do:

你还有另一种方法:

//inside the loop
if(!currentset.PostalCode.Equals("1990"))
{
    Console.WriteLine("Set: "+currentset);
}

OR

要么

//inside the loop
if(currentset.PostalCode.Equals("1990"))
    continue;
Console.WriteLine("Set: "+currentset);