[Solved] c# foreach loop formatting


Hmmm, I haven’t tested it. I hope it helps you. I would do it using two foreach inside the main while

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.

2

solved c# foreach loop formatting