title: Linq to entities sum of ICollection count over a group
body: <p>I am trying to create a list of aggregate data by site, using linq to entities.</p>
  
  <p>the code first model has a TrialParticipant class which contains a property representing a one to many relationship StudyCentre, and an ICollection of AdverseEvents, representing a many to one relationship.</p>
  
  <p>Amongst other details, I would like to know the count of AdverseEvents per StudyCentre.</p>
  
  <p>The code</p>
  
  <pre><code>var returnVal =
      (from participant in _db.TrialParticipants
       group participant by participant.StudyCentreId into centre
       select new StudyCentreStatistic
       { 
          TotalNo = centre.Count(),
          ...
          AdverseEventNo = centre.Sum(participant=&gt;participant.AdverseEvents.Count)
       }).ToList();
  </code></pre>
  
  <p>But this creates an error:</p>
  
  <p>There was an error parsing the query. [ Token line number = 72,Token line offset = 4,Token in error = SELECT</p>
  
  <p>Any ideas about how to do this would be much appreciated. Thank you.</p>