public ITableInfo VisitUnresolvedGroupReferenceTableInfo(UnresolvedGroupReferenceTableInfo tableInfo)
        {
            var groupSourceSubStatementTableInfo = tableInfo.ReferencedGroupSource.GetResolvedTableInfo() as ResolvedSubStatementTableInfo;

            if (groupSourceSubStatementTableInfo == null)
            {
                var message = string.Format(
                    "This SQL generator only supports sequences in from expressions if they are members of an entity or if they come from a GroupBy operator. "
                    + "Sequence: '{0}'", tableInfo);
                throw new NotSupportedException(message);
            }

            var groupingSelectExpression = groupSourceSubStatementTableInfo.SqlStatement.SelectProjection as SqlGroupingSelectExpression;

            if (groupingSelectExpression == null)
            {
                throw new NotSupportedException(
                          "When a sequence retrieved by a subquery is used in a from expression, the subquery must end with a GroupBy operator.");
            }

            var elementSelectingStatementBuilder = new SqlStatementBuilder(groupSourceSubStatementTableInfo.SqlStatement)
            {
                GroupByExpression = null
            };

            var currentKeyExpression = Expression.MakeMemberAccess(
                new SqlTableReferenceExpression(tableInfo.ReferencedGroupSource),
                groupingSelectExpression.Type.GetProperty("Key"));

            var groupKeyJoinCondition = _stage.ResolveWhereExpression(
                Expression.OrElse(
                    Expression.AndAlso(new SqlIsNullExpression(groupingSelectExpression.KeyExpression), new SqlIsNullExpression(currentKeyExpression)),
                    Expression.AndAlso(
                        Expression.AndAlso(
                            new SqlIsNotNullExpression(groupingSelectExpression.KeyExpression),
                            new SqlIsNotNullExpression(currentKeyExpression)),
                        Expression.Equal(groupingSelectExpression.KeyExpression, currentKeyExpression))),
                _context);

            elementSelectingStatementBuilder.AddWhereCondition(groupKeyJoinCondition);

            elementSelectingStatementBuilder.SelectProjection = groupingSelectExpression.ElementExpression;
            elementSelectingStatementBuilder.RecalculateDataInfo(groupingSelectExpression);

            return(new ResolvedJoinedGroupingTableInfo(
                       _generator.GetUniqueIdentifier("q"),
                       elementSelectingStatementBuilder.GetSqlStatement(),
                       groupingSelectExpression,
                       groupSourceSubStatementTableInfo.TableAlias));
        }
Exemple #2
0
        protected virtual Expression ResolveWhereCondition(Expression whereCondition)
        {
            ArgumentUtility.CheckNotNull("whereCondition", whereCondition);

            return(_stage.ResolveWhereExpression(whereCondition, _context));
        }