title: In EF Core, how can I perform a group by and select a list of comma separated values?
body: <p>In this little ASP.Net Core application, I am attempting to group students by their enrollment date and return the students' names in a comma separated string, as opposed to another kind of aggregation.</p>
  
  <p>When using SQL, I've used the <code>stuff</code> function in the past, and I haven't been able to perform the equivalent operation as a subquery in LINQ. </p>
  
  <p>Core won't allow me to perform client-side GroupBys, which I tried at first.</p>
  
  <blockquote>
    <p>Client side GroupBy is not supported.</p>
  </blockquote>
  
  <p>I've tried the following code and received an InvalidOperationException:</p>
  
  <pre><code>IQueryable&lt;EnrollmentDateGroup&gt; data =
          _context.Students
          .GroupBy(s =&gt; s.EnrollmentDate)
          .Select(s =&gt; new EnrollmentDateGroup()
          {
            EnrollmentDate = s.Key,
            StudentCount = s.Count(),
            //BELOW IS NOT WORKING
            StudentNamesCSV = string.Join(",", s.Select(x =&gt; x.FirstMidName + " " + x.LastName))
          });
  </code></pre>
  
  <p>Another attempt and error message, moving away from using the already grouped data:</p>
  
  <blockquote>
    <p>SqlException: Column 'Student.EnrollmentDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.</p>
  </blockquote>
  
  <pre><code>//ALSO NOT WORKING
  ...
  StudentNamesCSV = string.Join(",", _context.Students
    .Where(x =&gt; x.EnrollmentDate == s.Key)
    .Select(x =&gt; x.FirstMidName + " " + x.LastName))
  ...
  </code></pre>
  
  <p>Any ideas are welcome! Thank you.</p>