title: group by using anonymous type in Linq
body: <p>Let say I have an Employee class, and GetAllEmployees() return a list of employee instance. I want to group employees by Department and Gender, so the answer I have is </p>
  
  <pre><code>var employeeGroup = Employee.GetAllEmployees()
                              .GroupBy(x =&gt; new { x.Department, x.Gender }) // I don't understand this anonymous type
                              .OrderBy(g =&gt; g.Key.Department)
                              .ThenBy(g =&gt; g.Key.Gender)
                              .Select(g =&gt; new {   //I can understand this anonymous type
                                 Dept = g.Key.Department,
                                 Gender = g.Key.Gender,
                                 Employees = g.OrderBy(x =&gt; x.Name)
                               });
  </code></pre>
  
  <p>I have two questions:</p>
  
  <ol>
  <li><p>Why an anonymous type allows a multiple keys group by?</p></li>
  <li><p>I don't understand the first anonymous type, because from my understanding, an anonymous type's format should be like this</p>
  
  <p>new { field1 = x.Department, field2 = x.Gender }</p></li>
  </ol>
  
  <p>How come the first anonymous type can have without fields? I mean, it's correct syntax to write something like this:</p>
  
  <pre><code>var anonymous = new {field1 = 1,field2 =2}
  </code></pre>
  
  <p>But there will be compile error if I write it like this:</p>
  
  <pre><code>var anonymous = new {1, 2} //compile error !!!
  </code></pre>