title: Group Umbraco nodes based on doctype of AncestorOrSelf using LINQ
body: <p>I'm trying to group all the nodes in my Umbraco (v7) content tree using LINQ, but with one condition: if a node has a certain doctype, then this node and it's children will be grouped together.</p>
  
  <p>So, in the following example, the aliases in brackets represent the doctype alias of the node:</p>
  
  <pre><code>Page 1 ('Page')
      Child 1 ('Page')
  Page 2 ('Page')
  Page 3 ('Page')
  Page 4 ('Article')
      Child 2 ('Text')
      Child 3 ('Text')
      Child 4 ('Text')
  </code></pre>
  
  <p>Page 4 has a different doctype to Page 1, so Page 4 and it's children should be grouped together, whereas Page 1's child isn't grouped because it has the ('Page') doctype. The count of all nodes should be '8' and the filtered nodes count should be '5'.</p>
  
  <p>My first attempt at this query was to do the following:</p>
  
  <pre><code>var results = from x in nodes
                (x.DocumentTypeAlias == "Article" ? 
                    group x by x.AncestorOrSelf().Id into z : 
                    group x by x into z)
                select new{Id = z.Key};
  </code></pre>
  
  <p>However, this results in the error message <em>"Invalid expression term 'group'"</em>. Is there a solution this and/or is my logic completely wrong?</p>