title: Creating a multi-layered matrix-ish Collection in C#
body: <p><strong>The setup</strong></p>
  
  <p>I have a <code>List&lt;Room&gt;()</code> which I get back from a service. The list refreshes every 10 seconds, and rooms get added and removed.</p>
  
  <pre><code>class Room 
  {
      public int ID {get;set;}
  }
  </code></pre>
  
  <p><strong>My job</strong></p>
  
  <p>To display these rooms on the screen, I have a Matrix-like view of variable size. 
  Sometimes the matrix is 3 x 3 cells, other times it is 4 x 2 or 5 x 1.</p>
  
  <p>I needed a way to "remember" which slot/cell a room has been placed in, so I thought a DataTable would give me that option. </p>
  
  <p>To store the cells I use a DataTable, which has 3 Columns: </p>
  
  <ul>
  <li>"Column" (int)</li>
  <li>"Row" (int)</li>
  <li>"Room" (Room)</li>
  </ul>
  
  <p>So If I have a 2 x 4 matrix, it would look like this.</p>
  
  <pre>
  
   Column  |  Row  |  Room
  -----------------------------
      0    |   0   |  rooms[0] 
  -----------------------------
      1    |   0   |  rooms[1] 
  -----------------------------
      2    |   0   |  rooms[2] 
  -----------------------------
      0    |   1   |  rooms[3] 
  -----------------------------
      1    |   2   |  rooms[4] 
  
  And so forth...
  
  </pre>
  
  <p>Once I have this DataTable I am then able to refresh the screen, knowing that each room will be displayed at the position it was before. This can probably be achieved in a smarter way.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>Now I need to enumerate the <code>List&lt;Room&gt;</code> and fill the matrix/DataTable. </p>
  
  <p>If I have more rooms than cells, then I need to start at position <code>0,0</code> again (like adding a new matrix as a layer), until all rooms have been assigned a cell.</p>
  
  <p><strong>The approach so far</strong></p>
  
  <p>I have tried a few <code>for(...)</code> loops that look like:</p>
  
  <pre><code>int totalTiles = area.TileColumns * area.TileRows;
  int totalLayers = (int)Math.Ceiling((double)area.Rooms.Count / totalTiles);
  
  for (int i = 0; i &lt; totalLayers; i++)
  {
      for (int j = 0; j &lt; area.TileRows; j++)
      {
          for (int k = 0; k &lt; area.TileColumns; k++)
          {
              // This is going nowhere :-(
          }
      }
  }
  </code></pre>
  
  <p><strong>In my brain</strong></p>
  
  <p>When I first came across this problem, I immediately thought: <em>"Nothing a simple LINQ query won't fix!"</em>. And then I bricked ...</p>
  
  <p>What would be the most efficient / best performing approach to fill this matrix?</p>