[英]c# foreach loop formatting
I have the following C# code that provide the below output but I need some help to change it to the below desire output.我有以下 C# 代码提供以下输出,但我需要一些帮助才能将其更改为以下所需的输出。 How can I rewrite my code to provide the "Required output"?
如何重写我的代码以提供“必需的输出”?
SearchResultSet results = session.Search(searchRequest);
results.GetCount());
IEnumerable columns = results.GetColumns();
while (results.HasNext())
{
foreach (string column in columns)
{
Console.WriteLine(column + ": " + results.GetString(column));
}
Console.WriteLine();
}
Output:输出:
Unique_ID: 234556 Address: 555 John Street City: Orlando State: FL Zip: 32751 Unique_ID: 5326536 Address: 1200 Avenue of America City: New York State: NY Zip: 10016 Unique_ID: 4815585 Address: 149 Madison Avenue City: New York State: NY Zip: 10016
Required Output:所需输出:
Unique_ID|Address|City|State|Zip 234556|555 John Street|Orlando|FL|32751 5326536|1200 Avenue of America|New York|NY|10016 4815585|149 Madison Avenue|New York|NY|10016
Hmmm, I haven't tested it.嗯,我没有测试过。 I hope it helps you.
我希望它能帮助你。 I would do it using two
foreach
inside the main while
我会在 main
while
使用两个foreach
SearchResultSet results = session.Search(searchRequest);
results.GetCount());
IEnumerable columns = results.GetColumns();
bool printColumns = true;
while (results.HasNext())
{
if(printColumns){
foreach (string column in columns)
{
Console.WriteLine(String.Format("{0}|", column)); //Will print Unique_ID|Address|etc...
}
}
printColumns = false;
foreach (string column in columns)
{
Console.Write(String.Format("{0}|", results.GetString(column))); //Will print 234556|555 JOHN STREET|Orlando|FL|32751
}
Console.WriteLine();
}
EDIT:编辑:
The previous code keeps a pipe at the end of the strings.前面的代码在字符串的末尾保留了一个管道。 If you don't want this, you should use a
String
variable, concatenate the result, and after each foreach
, you simply do a susbtr to remove the ending pipe.如果你不想要这个,你应该使用一个
String
变量,连接结果,在每个foreach
,你只需执行一个 susbtr 来删除结束管道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.