/// <summary>
        ///     Determines whether the <see cref="Expression" /> is rooted.
        /// </summary>
        /// <param name="expression">
        ///     The expression.
        /// </param>
        /// <param name="expectedRoot">
        ///     The expected root.
        /// </param>
        /// <param name="data">
        ///     The <see cref="QueryHelperData" /> info.
        /// </param>
        /// <returns>
        ///     True if the <see cref="Expression" /> is rooted; false otherwise.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="expression" /> is null.
        /// </exception>
        public static bool IsRooted
        (
            Expression expression,
            string expectedRoot,
            QueryHelperData data
        )
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            string property = GetPropertyName(expression);

            string[] splits = property.Split('.');

            if (splits.Length <= 1)
            {
                return(false);
            }

            bool isRooted = splits[0] == expectedRoot ||
                            data.Aliases
                            .ContainsValue(splits[0]);

            if (isRooted)
            {
                return(true);
            }

            string tempAlias = string.Join("_", splits.Take(splits.Length - 1));

            return(data.Aliases
                   .ContainsValue(tempAlias));
        }
Example #2
0
        /// <summary>
        ///     Generates new orders based on the underlying source by resolving the appropriate projections from the
        ///     given lambda expression.
        /// </summary>
        /// <param name="orderByStatements">
        ///     The order by statements.
        /// </param>
        /// <param name="projection">
        ///     The projection.
        /// </param>
        /// <param name="data">
        ///     The helper data.
        /// </param>
        /// <returns>
        ///     The new set of orders.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="orderByStatements" />, <paramref name="projection" /> or <paramref name="data" /> is
        ///     null.
        /// </exception>
        public static IEnumerable <OrderByStatement> GetSourceBasedOrdersFrom
        (
            IEnumerable <OrderByStatement> orderByStatements,
            LambdaExpression projection,
            QueryHelperData data
        )
        {
            if (orderByStatements == null)
            {
                throw new ArgumentNullException("orderByStatements");
            }

            if (projection == null)
            {
                throw new ArgumentNullException("projection");
            }

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (orderByStatements.All(x => x.IsBasedOnSource))
            {
                return(orderByStatements);
            }

            ProjectionHelper.GetProjection(projection, new HelperContext(data, projection, HelperType.Select));

            var orders = new List <OrderByStatement>();

            foreach (var statement in orderByStatements)
            {
                if (statement.IsBasedOnSource)
                {
                    orders.Add(statement);

                    continue;
                }

                IProjection orderProjection;

                bool foundMapping = data.Mappings
                                    .TryGetValue(statement.Property, out orderProjection);

                if (foundMapping)
                {
                    var order = new OrderByStatement
                    {
                        IsBasedOnSource = true,
                        Order           = new Order(orderProjection, statement.OrderAscending)
                    };

                    orders.Add(order);
                }
            }

            return(orders);
        }
 /// <summary>
 ///     Creates a <see cref="IProjection" /> for the given <see cref="Expression" />.
 /// </summary>
 /// <param name="expression">
 ///     The expression.
 /// </param>
 /// <param name="data">
 ///     The <see cref="QueryHelperData" /> data.
 /// </param>
 /// <param name="type">
 ///     The <see cref="HelperType" /> type.
 /// </param>
 /// <returns>
 ///     The resolved <see cref="IProjection" /> instance.
 /// </returns>
 /// <exception cref="NotSupportedException">
 ///     The <see cref="Expression" /> could not be resolved as it may contain unsupported features or similar.
 /// </exception>
 public static IProjection GetProjection
 (
     LambdaExpression expression,
     QueryHelperData data,
     HelperType type = HelperType.Select
 )
 {
     return(GetProjection(expression.Body, new HelperContext(data, expression.Parameters[0].Name, type)));
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="HelperContext" /> class.
 /// </summary>
 /// <param name="data">
 ///     The <see cref="QueryHelperData" /> data.
 /// </param>
 /// <param name="rootAlias">
 ///     The root alias.
 /// </param>
 /// <param name="type">
 ///     The <see cref="HelperType" />.
 /// </param>
 public HelperContext
 (
     QueryHelperData data,
     string rootAlias,
     HelperType type
 )
 {
     Data      = data;
     RootAlias = rootAlias;
     Type      = type;
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="HelperContext" /> class.
 /// </summary>
 /// <param name="data">
 ///     The <see cref="QueryHelperData" /> data.
 /// </param>
 /// <param name="expression">
 ///     A lambda expression.
 /// </param>
 /// <param name="type">
 ///     The <see cref="HelperType" />.
 /// </param>
 public HelperContext
 (
     QueryHelperData data,
     LambdaExpression expression,
     HelperType type
 )
     : this(data, (string)null, type)
 {
     RootAlias = expression != null && expression.Parameters.Count > 0
         ? expression.Parameters[0].Name
         : null;
 }