private static ObjectQueryState BuildSelectOrSelectValue(
            ObjectQueryState query,
            string alias,
            string projection,
            ObjectParameter[] parameters,
            string projectOp,
            Type elementType)
        {
            string        commandText = EntitySqlQueryBuilder.GetCommandText(query);
            StringBuilder queryText   = new StringBuilder(projectOp.Length + projection.Length + "\r\nFROM (\r\n".Length + commandText.Length + "\r\n) AS ".Length + alias.Length);

            queryText.Append(projectOp);
            queryText.Append(projection);
            queryText.Append("\r\nFROM (\r\n");
            queryText.Append(commandText);
            queryText.Append("\r\n) AS ");
            queryText.Append(alias);
            return(EntitySqlQueryBuilder.NewBuilderQuery(query, elementType, queryText, (Span)null, (IEnumerable <ObjectParameter>)EntitySqlQueryBuilder.MergeParameters(query.ObjectContext, query.Parameters, parameters)));
        }
        internal static ObjectQueryState OfType(
            ObjectQueryState query,
            EdmType newType,
            Type clrOfType)
        {
            string        commandText = EntitySqlQueryBuilder.GetCommandText(query);
            StringBuilder queryText   = new StringBuilder("OFTYPE(\r\n(\r\n".Length + commandText.Length + "\r\n),\r\n[".Length + newType.NamespaceName.Length + (!string.IsNullOrEmpty(newType.NamespaceName) ? "].[".Length : 0) + newType.Name.Length + "]\r\n)".Length);

            queryText.Append("OFTYPE(\r\n(\r\n");
            queryText.Append(commandText);
            queryText.Append("\r\n),\r\n[");
            if (!string.IsNullOrEmpty(newType.NamespaceName))
            {
                queryText.Append(newType.NamespaceName);
                queryText.Append("].[");
            }
            queryText.Append(newType.Name);
            queryText.Append("]\r\n)");
            return(EntitySqlQueryBuilder.NewBuilderQuery(query, clrOfType, queryText, query.Span, (IEnumerable <ObjectParameter>)ObjectParameterCollection.DeepCopy(query.Parameters)));
        }
        internal static ObjectQueryState GroupBy(
            ObjectQueryState query,
            string alias,
            string keys,
            string projection,
            ObjectParameter[] parameters)
        {
            string        commandText = EntitySqlQueryBuilder.GetCommandText(query);
            StringBuilder queryText   = new StringBuilder("SELECT ".Length + projection.Length + "\r\nFROM (\r\n".Length + commandText.Length + "\r\n) AS ".Length + alias.Length + "\r\nGROUP BY\r\n".Length + keys.Length);

            queryText.Append("SELECT ");
            queryText.Append(projection);
            queryText.Append("\r\nFROM (\r\n");
            queryText.Append(commandText);
            queryText.Append("\r\n) AS ");
            queryText.Append(alias);
            queryText.Append("\r\nGROUP BY\r\n");
            queryText.Append(keys);
            return(EntitySqlQueryBuilder.NewBuilderQuery(query, typeof(DbDataRecord), queryText, (Span)null, (IEnumerable <ObjectParameter>)EntitySqlQueryBuilder.MergeParameters(query.ObjectContext, query.Parameters, parameters)));
        }
        private static ObjectQueryState BuildSetOp(
            ObjectQueryState leftQuery,
            ObjectQueryState rightQuery,
            Span newSpan,
            string setOp)
        {
            string commandText1 = EntitySqlQueryBuilder.GetCommandText(leftQuery);
            string commandText2 = EntitySqlQueryBuilder.GetCommandText(rightQuery);

            if (!object.ReferenceEquals((object)leftQuery.ObjectContext, (object)rightQuery.ObjectContext))
            {
                throw new ArgumentException(Strings.ObjectQuery_QueryBuilder_InvalidQueryArgument, "query");
            }
            StringBuilder queryText = new StringBuilder("(\r\n".Length + commandText1.Length + setOp.Length + commandText2.Length + "\r\n)".Length);

            queryText.Append("(\r\n");
            queryText.Append(commandText1);
            queryText.Append(setOp);
            queryText.Append(commandText2);
            queryText.Append("\r\n)");
            return(EntitySqlQueryBuilder.NewBuilderQuery(leftQuery, leftQuery.ElementType, queryText, newSpan, (IEnumerable <ObjectParameter>)EntitySqlQueryBuilder.MergeParameters(leftQuery.Parameters, rightQuery.Parameters)));
        }
        internal static ObjectQueryState Top(
            ObjectQueryState query,
            string alias,
            string count,
            ObjectParameter[] parameters)
        {
            int           length               = count.Length;
            string        commandText          = EntitySqlQueryBuilder.GetCommandText(query);
            bool          allowsLimitSubclause = ((EntitySqlQueryState)query).AllowsLimitSubclause;
            StringBuilder queryText            = new StringBuilder(!allowsLimitSubclause ? length + ("SELECT VALUE TOP(\r\n".Length + "\r\n) ".Length + alias.Length + "\r\nFROM (\r\n".Length + commandText.Length + "\r\n) AS ".Length + alias.Length) : length + (commandText.Length + "\r\nLIMIT\r\n".Length));

            if (allowsLimitSubclause)
            {
                queryText.Append(commandText);
                queryText.Append("\r\nLIMIT\r\n");
                queryText.Append(count);
            }
            else
            {
                queryText.Append("SELECT VALUE TOP(\r\n");
                queryText.Append(count);
                queryText.Append("\r\n) ");
                queryText.Append(alias);
                queryText.Append("\r\nFROM (\r\n");
                queryText.Append(commandText);
                queryText.Append("\r\n) AS ");
                queryText.Append(alias);
            }
            return(EntitySqlQueryBuilder.NewBuilderQuery(query, query.ElementType, queryText, query.Span, (IEnumerable <ObjectParameter>)EntitySqlQueryBuilder.MergeParameters(query.ObjectContext, query.Parameters, parameters)));
        }
 internal static ObjectQueryState Except(
     ObjectQueryState leftQuery,
     ObjectQueryState rightQuery)
 {
     return(EntitySqlQueryBuilder.BuildSetOp(leftQuery, rightQuery, leftQuery.Span, "\r\n) EXCEPT (\r\n"));
 }
        private static ObjectQueryState BuildOrderByOrWhere(
            ObjectQueryState query,
            string alias,
            string predicateOrKeys,
            ObjectParameter[] parameters,
            string op,
            string skipCount,
            bool allowsLimit)
        {
            string commandText = EntitySqlQueryBuilder.GetCommandText(query);
            int    capacity    = "SELECT VALUE ".Length + alias.Length + "\r\nFROM (\r\n".Length + commandText.Length + "\r\n) AS ".Length + alias.Length + op.Length + predicateOrKeys.Length;

            if (skipCount != null)
            {
                capacity += "\r\nSKIP\r\n".Length + skipCount.Length;
            }
            StringBuilder queryText = new StringBuilder(capacity);

            queryText.Append("SELECT VALUE ");
            queryText.Append(alias);
            queryText.Append("\r\nFROM (\r\n");
            queryText.Append(commandText);
            queryText.Append("\r\n) AS ");
            queryText.Append(alias);
            queryText.Append(op);
            queryText.Append(predicateOrKeys);
            if (skipCount != null)
            {
                queryText.Append("\r\nSKIP\r\n");
                queryText.Append(skipCount);
            }
            return(EntitySqlQueryBuilder.NewBuilderQuery(query, query.ElementType, queryText, allowsLimit, query.Span, (IEnumerable <ObjectParameter>)EntitySqlQueryBuilder.MergeParameters(query.ObjectContext, query.Parameters, parameters)));
        }