Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I guess this is similar to Marc's answer, but I'll post it since I spent some time working on it. The results are separated by <code>" | "</code> as in your example. It also uses the <code>IGrouping&lt;int, string&gt;</code> type returned from the LINQ query when using a group by instead of constructing a new anonymous type. This is tested, working code.</p> <pre><code>var Items = new[] { new { TypeCode = 1, UserName = "Don Smith"}, new { TypeCode = 1, UserName = "Mike Jones"}, new { TypeCode = 1, UserName = "James Ray"}, new { TypeCode = 2, UserName = "Tom Rizzo"}, new { TypeCode = 2, UserName = "Alex Homes"}, new { TypeCode = 3, UserName = "Andy Bates"} }; var Columns = from i in Items group i.UserName by i.TypeCode; Dictionary&lt;int, List&lt;string&gt;&gt; Rows = new Dictionary&lt;int, List&lt;string&gt;&gt;(); int RowCount = Columns.Max(g =&gt; g.Count()); for (int i = 0; i &lt;= RowCount; i++) // Row 0 is the header row. { Rows.Add(i, new List&lt;string&gt;()); } int RowIndex; foreach (IGrouping&lt;int, string&gt; c in Columns) { Rows[0].Add(c.Key.ToString()); RowIndex = 1; foreach (string user in c) { Rows[RowIndex].Add(user); RowIndex++; } for (int r = RowIndex; r &lt;= Columns.Count(); r++) { Rows[r].Add(string.Empty); } } foreach (List&lt;string&gt; row in Rows.Values) { Console.WriteLine(row.Aggregate((current, next) =&gt; current + " | " + next)); } Console.ReadLine(); </code></pre> <p>I also tested it with this input:</p> <pre><code>var Items = new[] { new { TypeCode = 1, UserName = "Don Smith"}, new { TypeCode = 3, UserName = "Mike Jones"}, new { TypeCode = 3, UserName = "James Ray"}, new { TypeCode = 2, UserName = "Tom Rizzo"}, new { TypeCode = 2, UserName = "Alex Homes"}, new { TypeCode = 3, UserName = "Andy Bates"} }; </code></pre> <p>Which produced the following results showing that the first column doesn't need to contain the longest list. You could use <code>OrderBy</code> to get the columns ordered by TypeCode if needed. </p> <pre><code>1 | 3 | 2 Don Smith | Mike Jones | Tom Rizzo | James Ray | Alex Homes | Andy Bates | </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload