protected IQueryable ExecutePaging(IQueryable source, QueryContext context) {
            if (CanPage && AutoPage) {
                if (CanRetrieveTotalRowCount && context.Arguments.RetrieveTotalRowCount) {
                    context.Arguments.TotalRowCount = _queryable.Count(source);
                }

                if ((context.Arguments.MaximumRows > 0) && (context.Arguments.StartRowIndex >= 0)) {
                    source = _queryable.Skip(source, context.Arguments.StartRowIndex);
                    source = _queryable.Take(source, context.Arguments.MaximumRows);
                }
            }
            else if (context.Arguments.RetrieveTotalRowCount && (context.Arguments.TotalRowCount == -1)) {
                throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                                    AtlasWeb.LinqDataSourceView_PagingNotHandled, _owner.ID));
            }
            return source;
        }
        protected IQueryable ExecuteQueryExpressions(IQueryable source, QueryContext context) {
            if (source != null) {                
                QueryCreatedEventArgs queryArgs = new QueryCreatedEventArgs(source);
                OnQueryCreated(queryArgs);
                source = queryArgs.Query ?? source;

                // Support the Dynamic Expression language used by LinqDataSource
                if (AutoGenerateWhereClause) {
                    if (!String.IsNullOrEmpty(Where)) {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                        AtlasWeb.LinqDataSourceView_WhereAlreadySpecified, _owner.ID));
                    }
                    source = QueryableDataSourceHelper.CreateWhereExpression(context.WhereParameters, source, _queryable);
                }
                else if (!String.IsNullOrEmpty(Where)) {
                    source = _queryable.Where(source, Where, context.WhereParameters.ToEscapedParameterKeys(_owner));
                }

                if (AutoGenerateOrderByClause) {
                    if (!String.IsNullOrEmpty(OrderBy)) {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                        AtlasWeb.LinqDataSourceView_OrderByAlreadySpecified, _owner.ID));
                    }
                    source = QueryableDataSourceHelper.CreateOrderByExpression(context.OrderByParameters, source, _queryable);
                }
                else if (!String.IsNullOrEmpty(OrderBy)) {
                    source = _queryable.OrderBy(source, OrderBy, context.OrderByParameters.ToEscapedParameterKeys(_owner));
                }


                string groupBy = GroupBy;
                if (String.IsNullOrEmpty(groupBy)) {
                    if (!String.IsNullOrEmpty(OrderGroupsBy)) {
                        throw new InvalidOperationException(String.Format(CultureInfo.InvariantCulture,
                            AtlasWeb.LinqDataSourceView_OrderGroupsByRequiresGroupBy, _owner.ID));
                    }
                }
                else {
                    source = _queryable.GroupBy(source, groupBy, "it", context.GroupByParameters.ToEscapedParameterKeys(_owner));
                    if (!String.IsNullOrEmpty(OrderGroupsBy)) {
                        source = _queryable.OrderBy(source, OrderGroupsBy, context.OrderGroupsByParameters.ToEscapedParameterKeys(_owner));
                    }
                }

                if (!String.IsNullOrEmpty(SelectNew)) {
                    source = _queryable.Select(source, SelectNew, context.SelectParameters.ToEscapedParameterKeys(_owner));
                }

                return source;
            }

            return source;
        }
        protected IQueryable ExecuteSorting(IQueryable source, QueryContext context) {
            string sortExpression = context.Arguments.SortExpression;

            if (CanSort && AutoSort && !String.IsNullOrEmpty(sortExpression)) {
                source = _queryable.OrderBy(source, sortExpression);
            }
            return source;
        }
        protected virtual IQueryable ExecuteQuery(IQueryable source, QueryContext context) {
            // Execute Query
            source = ExecuteQueryExpressions(source, context);

            // Execute Sorting
            source = ExecuteSorting(source, context);

            // Execute Paging
            source = ExecutePaging(source, context);

            return source;
        }
 /// <summary>
 /// Gets the result to apply query
 /// </summary>
 /// <returns></returns>
 protected abstract object GetSource(QueryContext context);
        protected override IQueryable ExecuteQuery(IQueryable source, QueryContext context)
        {
            // If we're not supposed to retrieve the total row count, just call the base
            if (!context.Arguments.RetrieveTotalRowCount)
                return base.ExecuteQuery(source, context);

            // Turn that off so that the base implementation won't make a count request
            context.Arguments.RetrieveTotalRowCount = false;

            // Call the base to build the query
            source = base.ExecuteQuery(source, context);

            // Include the total Row Count as part of the data query, to avoid making two separate queries
            MethodInfo includeTotalCountMethod = source.GetType().GetMethod("IncludeTotalCount");
            if (includeTotalCountMethod == null)
                return source;

            source = (IQueryable)includeTotalCountMethod.Invoke(source, null);

            // Execute the query
            MethodInfo executeMethod = source.GetType().GetMethod("Execute");
            var queryOperationResponse = (QueryOperationResponse)executeMethod.Invoke(source, null);

            // Get the count and set it in the Arguments
            context.Arguments.TotalRowCount = (int)queryOperationResponse.TotalCount;

            // Return it as an IQueryable.
            // Note that we end up returning an executed query, while the base implementation doesn't.
            // But this should be harmless, since all the LINQ operations were already included in the query
            return queryOperationResponse.AsQueryable();
        }
        protected override object GetSource(QueryContext context) {
            LinqDataSourceSelectEventArgs selectEventArgs = new LinqDataSourceSelectEventArgs(
                context.Arguments,
                context.WhereParameters,
                context.OrderByParameters,
                context.GroupByParameters,
                context.OrderGroupsByParameters,
                context.SelectParameters);

            OnSelecting(selectEventArgs);
            if (selectEventArgs.Cancel) {
                return null;
            }

            _selectResult = selectEventArgs.Result;
            object table = _selectResult;

            // Original values should only be stored for valid delete and update scenarios.
            _storeOriginalValues = StoreOriginalValuesInViewState && (CanDelete || CanUpdate) &&
                String.IsNullOrEmpty(GroupBy) && String.IsNullOrEmpty(SelectNew);

            if (_selectResult == null) {
                table = base.GetSource(context);
                _selectResult = table;
            }
            // If the provided select result was not a DLinq table and we need to store
            // original values then we must get the table and create a new data context
            // instance so that we can access the column metadata.
            else if (!(table is ITable) && _storeOriginalValues) {
                table = base.GetSource(context);
            }

            return table;
        }
        protected override object GetSource(QueryContext context) {
            ContextDataSourceContextData contextData = CreateContext(DataSourceOperation.Select);
            if (contextData != null) {
                // Set the current context
                Context = contextData.Context;
                EntitySet = contextData.EntitySet;
                return EntitySet;
            }

            return null;
        }