// join templates and documents on the template ID and group documents by the template

varusedTemplates =

(

from

template in allTemplates

join

doc in docSource.XPathSelectElements("//NewDataSet/*[DocumentPageTemplateID]") //use * is because actual entries have different names, use [condition] is because not every document has a template (could inherit from parent instead)