title: Linq Queries with multiple group by and Scalar valued function
body: <p>I am trying to write a LINQ query, having multiple group by and scalar valued functions using Entity Framework. </p>
  
  <p>This is a sample query with simpler names:</p>
  
  <pre><code>var test = context.&lt;db_view&gt;.Where(predicate)
                              .GroupBy(x =&gt; new {x.col1, x.col2, x.col3})
                              .Select(y =&gt; new
                                           {
                                               a = y.key.col1,
                                               b = y.key.col2,
                                               c = y.key.col3,
                                               d = ctx.ScalarFunction(y.key.col2)
                                           });
  </code></pre>
  
  <p>I however get an error:</p>
  
  <pre><code>"Column Distinct1.col1 is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
  </code></pre>
  
  <p>I do have col1 in the GROUP BY Clause. Am I missing something here?</p>
  
  <p>This is the SQL Query generated by Entity Framework:</p>
  
  <pre><code>SELECT 
      1 AS [C1], 
      [Distinct1].[col1] AS [col1], 
      [Distinct1].[col2] AS [col2], 
      [Distinct1].[col3] AS [col3], 
      [dbo].[scalarfunction]([Distinct1].[col2]) AS [C2], 
      FROM ( SELECT DISTINCT 
          [Extent1].[col1] AS [col1], 
          [Extent1].[col2] AS [col2], 
          [Extent1].[col3] AS [col3], 
          FROM (SELECT 
      [view].[col1] AS [col1], 
      [view].[col2] AS [col2], 
      [view].[col3] AS [col3], 
      [view].[col4] AS [col4], 
      [view].[col5] AS [col5], 
      [view].[col6] AS [col6] 
  
      FROM [dbo].[view] AS [view]) AS [Extent1]
          WHERE (predicate
      )  AS [Distinct1]
  </code></pre>