//In SelectGenerator public override SqlFragment Visit(DbGroupByExpression expression) { // first process the input DbGroupExpressionBinding e = expression.Input; SelectStatement innerSelect = VisitInputExpressionEnsureSelect(e.Expression, e.VariableName, e.VariableType); SelectStatement select = WrapIfNotCompatible(innerSelect, expression.ExpressionKind); CollectionType ct = (CollectionType)expression.ResultType.EdmType; RowType rt = (RowType)ct.TypeUsage.EdmType; int propIndex = 0; foreach (DbExpression key in expression.Keys) { var tmp = key.Accept(this) as ColumnFragment; select.AddGroupBy(tmp); propIndex++; /* Sorry for my English. * we must add to the 'select' expression each field, which is involved in 'groupby'. * Each field can be selected and we do not know exactly which of them (without analysis). * Simply select all. The main thing is how you are building the alias for the fields. I don't know. * * * */ tmp = tmp.Clone(); tmp.ColumnAlias = String.Format("K{0}", propIndex); select.Columns.Add(tmp); } for (int agg = 0; agg < expression.Aggregates.Count; agg++) { DbAggregate a = expression.Aggregates[agg]; DbFunctionAggregate fa = a as DbFunctionAggregate; if (fa == null) throw new NotSupportedException(); string alias = rt.Properties[propIndex++].Name; ColumnFragment functionCol = new ColumnFragment(null, null); functionCol.Literal = HandleFunction(fa, a.Arguments[0].Accept(this)); functionCol.ColumnAlias = alias; select.Columns.Add(functionCol); } return select; } //more correctly be foreach (DbExpression key in expression.Keys) { var tmp = key.Accept(this); select.AddGroupBy(tmp); propIndex++; var keyTmp = tmp as ColumnFragment; if(keyTmp != null) { keyTmp = keyTmp.Clone(); keyTmp.ColumnAlias = String.Format("K{0}", propIndex); select.Columns.Add(keyTmp); } } //In class ColumnFragment public ColumnFragment Clone() { ColumnFragment cf = new ColumnFragment(TableName, ColumnName); cf.ColumnAlias = ColumnAlias; cf.TableAlias = TableAlias; //(in your v 6.4.0.0 this line is missing) cf.Literal = Literal; return cf; }