/// <summary>
        /// Apply the query information to a LINQ statement
        /// </summary>
        /// <typeparam name="T">Model type</typeparam>
        /// <param name="instance">constraints instance</param>
        /// <param name="dbQuery">LINQ queryable</param>
        /// <returns>Modified query</returns>
        public static IQueryable <T> ApplyTo <T>(this IQueryConstraints <T> instance, DbQuery <T> dbQuery) where T : class
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            if (dbQuery == null)
            {
                throw new ArgumentNullException(nameof(dbQuery));
            }

            foreach (string item in instance.IncludePaths)
            {
                dbQuery = dbQuery.Include(item);
            }

            IQueryable <T> query = dbQuery.AsNoTracking();

            if (instance.Predicate != null)
            {
                query = query.Where(instance.Predicate);
            }

            IOrderedQueryable <T> orderedQuery = null;

            for (int i = 0; i < instance.SortOrder.Count(); i++)
            {
                SortOrderEntry item = instance.SortOrder.ElementAt(i);
                if (i == 0)
                {
                    orderedQuery = item.SortOrder == SortOrderEnum.Ascending
                        ? query.OrderBy(item.SortPropertyName)
                        : query.OrderByDescending(item.SortPropertyName);
                }
                else
                {
                    orderedQuery = item.SortOrder == SortOrderEnum.Ascending
                        ? orderedQuery.ThenBy(item.SortPropertyName)
                        : orderedQuery.ThenByDescending(item.SortPropertyName);
                }
            }

            query = orderedQuery ?? query;

            if (instance.SortOrder.Any() && instance.PageNumber >= 1)
            {
                query = query.Skip(instance.StartRecord).Take(instance.PageSize);
            }

            return(query);
        }
Exemple #2
0
        protected override void InternalProcessRecord()
        {
            ObjectType[] array      = null;
            int          totalCount = 0;
            int          num        = 0;

            try
            {
                int         num2        = 3;
                QueryFilter queryFilter = DateTimeConverter.ConvertQueryFilter(this.innerFilter);
                while (num2-- > 0)
                {
                    try
                    {
                        if (base.BookmarkObject != null)
                        {
                            ObjectType bookmarkObject = base.BookmarkObject;
                            bookmarkObject.ConvertDatesToUniversalTime();
                        }
                        QueueViewerInputObject queueViewerInputObject = new QueueViewerInputObject((base.BookmarkIndex <= 0) ? -1 : (base.BookmarkIndex - 1), base.BookmarkObject, base.IncludeBookmark, this.IncludeLatencyInfo.IsPresent, this.IncludeDetails.IsPresent, base.ResultSize.IsUnlimited ? int.MaxValue : base.ResultSize.Value, queryFilter, base.SearchForward, base.SortOrder);
                        using (QueueViewerClient <ObjectType> queueViewerClient = new QueueViewerClient <ObjectType>((string)this.Server))
                        {
                            if (!VersionedQueueViewerClient.UsePropertyBagBasedAPI(this.targetServer))
                            {
                                PagedDataObject bookmarkObject2 = null;
                                if (typeof(ObjectType) == typeof(ExtensibleQueueInfo))
                                {
                                    if (base.Filter != null)
                                    {
                                        this.innerFilter = new MonadFilter(base.Filter, this, ObjectSchema.GetInstance <QueueInfoSchema>()).InnerFilter;
                                    }
                                    this.InitializeInnerFilter <QueueInfo>(null, QueueInfoSchema.Identity);
                                    if (base.BookmarkObject != null)
                                    {
                                        bookmarkObject2 = new QueueInfo(base.BookmarkObject as ExtensibleQueueInfo);
                                    }
                                }
                                else
                                {
                                    if (base.Filter != null)
                                    {
                                        this.innerFilter = new MonadFilter(base.Filter, this, ObjectSchema.GetInstance <MessageInfoSchema>()).InnerFilter;
                                    }
                                    this.InitializeInnerFilter <MessageInfo>(MessageInfoSchema.Identity, MessageInfoSchema.Queue);
                                    if (base.BookmarkObject != null)
                                    {
                                        bookmarkObject2 = new MessageInfo(base.BookmarkObject as ExtensibleMessageInfo);
                                    }
                                }
                                queueViewerInputObject.QueryFilter = this.innerFilter;
                                List <SortOrderEntry> list = null;
                                if (queueViewerInputObject.SortOrder != null)
                                {
                                    list = new List <SortOrderEntry>();
                                    foreach (QueueViewerSortOrderEntry queueViewerSortOrderEntry in queueViewerInputObject.SortOrder)
                                    {
                                        list.Add(SortOrderEntry.Parse(queueViewerSortOrderEntry.ToString()));
                                    }
                                }
                                array = queueViewerClient.GetQueueViewerObjectPage(queueViewerInputObject.QueryFilter, (queueViewerInputObject.SortOrder != null) ? list.ToArray() : null, queueViewerInputObject.SearchForward, queueViewerInputObject.PageSize, bookmarkObject2, queueViewerInputObject.BookmarkIndex, queueViewerInputObject.IncludeBookmark, queueViewerInputObject.IncludeDetails, queueViewerInputObject.IncludeComponentLatencyInfo, ref totalCount, ref num);
                            }
                            else if (VersionedQueueViewerClient.UseCustomSerialization(this.targetServer))
                            {
                                array = queueViewerClient.GetPropertyBagBasedQueueViewerObjectPageCustomSerialization(queueViewerInputObject, ref totalCount, ref num);
                            }
                            else
                            {
                                array = queueViewerClient.GetPropertyBagBasedQueueViewerObjectPage(queueViewerInputObject, ref totalCount, ref num);
                            }
                            break;
                        }
                    }
                    catch (RpcException ex)
                    {
                        if ((ex.ErrorCode != 1753 && ex.ErrorCode != 1727) || num2 == 0)
                        {
                            throw;
                        }
                    }
                }
                if (base.ReturnPageInfo)
                {
                    base.WriteObject(new PagedPositionInfo(num + 1, totalCount));
                }
                DateTimeConverter.ConvertResultSet <ObjectType>(array);
                foreach (ObjectType dataObject in array)
                {
                    base.WriteResult(dataObject);
                }
            }
            catch (ParsingNonFilterablePropertyException ex2)
            {
                base.WriteError(ex2, ErrorCategory.InvalidArgument, ex2.PropertyName);
            }
            catch (QueueViewerException ex3)
            {
                base.WriteError(ErrorMapper.GetLocalizedException(ex3.ErrorCode, null, this.Server), ErrorCategory.InvalidOperation, null);
            }
            catch (RpcException ex4)
            {
                base.WriteError(ErrorMapper.GetLocalizedException(ex4.ErrorCode, null, this.Server), ErrorCategory.InvalidOperation, null);
            }
        }