Пример #1
0
        /// <summary>
        /// Produces the Linq expression representing the entire filter descriptors collection.
        /// </summary>
        /// <param name="filterDescriptors">Collection of filters</param>
        /// <param name="filterOperator">The operator used to combine filters</param>
        /// <param name="expressionCache">Cache for storing built expressions</param>
        /// <returns>Produced linq expression, which can be <c>null</c> if there are no filter descriptors.</returns>
        public static Expression BuildFiltersExpression(
            FilterDescriptorCollection filterDescriptors,
            FilterDescriptorLogicalOperator filterOperator,
            ExpressionCache expressionCache)
        {
            Debug.Assert(filterDescriptors != null, "Unexpected null filterDescriptors");

            Expression filtersExpression = null;

            foreach (FilterDescriptor filterDescriptor in filterDescriptors)
            {
                // Ignored filters will not have a cache entry
                if (expressionCache.ContainsKey(filterDescriptor))
                {
                    Expression filterExpression = expressionCache[filterDescriptor];

                    if (filtersExpression == null)
                    {
                        filtersExpression = filterExpression;
                    }
                    else if (filterOperator == FilterDescriptorLogicalOperator.And)
                    {
                        filtersExpression = Expression.AndAlso(filtersExpression, filterExpression);
                    }
                    else
                    {
                        filtersExpression = Expression.OrElse(filtersExpression, filterExpression);
                    }
                }
            }

            return(filtersExpression);
        }
Пример #2
0
        /// <summary>
        /// Composes an <see cref="EntityQuery" /> for sorting and grouping purposes.
        /// </summary>
        /// <param name="source">The queryable source.</param>
        /// <param name="groupDescriptors">The group descriptors.</param>
        /// <param name="sortDescriptors">The sort descriptors.</param>
        /// <param name="expressionCache">Cache for storing built expressions</param>
        /// <returns>The composed <see cref="EntityQuery" />.</returns>
        public static EntityQuery OrderBy(
            EntityQuery source,
            GroupDescriptorCollection groupDescriptors,
            SortDescriptorCollection sortDescriptors,
            ExpressionCache expressionCache)
        {
            Debug.Assert(source != null, "Unexpected null source");
            Debug.Assert(sortDescriptors != null, "Unexpected null sortDescriptors");
            Debug.Assert(groupDescriptors != null, "Unexpected null groupDescriptors");

            bool hasOrderBy = false;

            // check the GroupDescriptors first
            foreach (GroupDescriptor groupDescriptor in groupDescriptors)
            {
                if (groupDescriptor != null && groupDescriptor.PropertyPath != null)
                {
                    Debug.Assert(expressionCache.ContainsKey(groupDescriptor), "There should be a cached group expression");

                    // check to see if we sort by the same parameter in desc order
                    bool sortAsc = true;
                    foreach (SortDescriptor sortDescriptor in sortDescriptors)
                    {
                        if (sortDescriptor != null)
                        {
                            string sortDescriptorPropertyPath  = sortDescriptor.PropertyPath;
                            string groupDescriptorPropertyPath = groupDescriptor.PropertyPath;

                            if (sortDescriptorPropertyPath != null &&
                                sortDescriptorPropertyPath.Equals(groupDescriptorPropertyPath))
                            {
                                if (sortDescriptor.Direction == ListSortDirection.Descending)
                                {
                                    sortAsc = false;
                                }

                                break;
                            }
                        }
                    }

                    string orderMethodName = (!hasOrderBy ? "OrderBy" : "ThenBy");
                    if (!sortAsc)
                    {
                        orderMethodName += "Descending";
                    }

                    source     = OrderBy(source, orderMethodName, expressionCache[groupDescriptor]);
                    hasOrderBy = true;
                }
            }

            // then check the SortDescriptors
            foreach (SortDescriptor sortDescriptor in sortDescriptors)
            {
                if (sortDescriptor != null)
                {
                    Debug.Assert(expressionCache.ContainsKey(sortDescriptor), "There should be a cached sort expression");

                    string orderMethodName = (!hasOrderBy ? "OrderBy" : "ThenBy");
                    if (sortDescriptor.Direction == ListSortDirection.Descending)
                    {
                        orderMethodName += "Descending";
                    }

                    source     = OrderBy(source, orderMethodName, expressionCache[sortDescriptor]);
                    hasOrderBy = true;
                }
            }

            return(source);
        }