KgOfHedgehogs KgOfHedgehogs - 1 year ago 82
C# Question

c# long line of code formatting

I wrote this line of code. It is unreadable. Is there clever way to break down it into multiple lines of code < 80 or 100 characters length?

Console.WriteLine(String.Join("\n", testResults.Select(row => String.Join("|", row.Select(column => String.Format("{0,20}", column.ToString()))))));

Answer Source

I suggest separating query itself and its final representation (console output):

// Query: what to output
var testReport = testResults
  .Select(row => String.Join("|", row
     .Select(column => String.Format("{0,20}", column)))); // .ToString() is redundant

// Representation: how to output (print on the console in one go)
Console.WriteLine(String.Join(Environment.NewLine, testReport));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download