Remotion.Linq.SqlBackend.MappingResolution.GroupAggregateSimplifier.SimplifyIfPossible C# (CSharp) Method

SimplifyIfPossible() public method

public SimplifyIfPossible ( SqlSubStatementExpression subStatementExpression, Expression unresolvedSelectProjection ) : Expression
subStatementExpression Remotion.Linq.SqlBackend.SqlStatementModel.SqlSubStatementExpression
unresolvedSelectProjection System.Linq.Expressions.Expression
return System.Linq.Expressions.Expression
    public Expression SimplifyIfPossible (
        SqlSubStatementExpression subStatementExpression,
        Expression unresolvedSelectProjection)
    {
      ArgumentUtility.CheckNotNull ("subStatementExpression", subStatementExpression);
      ArgumentUtility.CheckNotNull ("unresolvedSelectProjection", unresolvedSelectProjection);

      var resolvedSqlStatement = subStatementExpression.SqlStatement;
      if (IsSimplifiableGroupAggregate (resolvedSqlStatement))
      {
        var joinedGroupingTableInfo = (ResolvedJoinedGroupingTableInfo) resolvedSqlStatement.SqlTables[0].GetResolvedTableInfo ();

        // Strip surrounding names so that there won't be a named expression inside the new aggregation
        var elementExpression = _context.RemoveNamesAndUpdateMapping (joinedGroupingTableInfo.AssociatedGroupingSelectExpression.ElementExpression);
        var visitor = new SimplifyingVisitor (resolvedSqlStatement.SqlTables[0], elementExpression);

        var aggregationExpression = FindAggregationExpression (unresolvedSelectProjection);
        if (aggregationExpression == null)
        {
          throw new ArgumentException (
              "The unresolved projection doesn't match the resolved statement: it has no aggregation.",
              "unresolvedSelectProjection");
        }
        var newAggregation = visitor.VisitExpression (aggregationExpression);

        if (visitor.CanBeTransferredToGroupingSource)
        {
          var resolvedNewAggregation = _stage.ResolveAggregationExpression (newAggregation, _context);

          var aggregationName = joinedGroupingTableInfo.AssociatedGroupingSelectExpression.AddAggregationExpressionWithName (resolvedNewAggregation);

          return new SqlColumnDefinitionExpression (
              resolvedSqlStatement.SelectProjection.Type,
              joinedGroupingTableInfo.GroupSourceTableAlias,
              aggregationName,
              false);
        }
      }

      return subStatementExpression;
    }