private void PerformAutoLogic(esDataSourceSelectEventArgs e)
        {
            esDynamicQuery query = e.Query != null ? e.Query : e.Collection.es.Query;

            IDynamicQuerySerializableInternal iQuery = query as IDynamicQuerySerializableInternal;

            if (this.autoPaging)
            {
                query.es.PageNumber = e.PageNumber;
                query.es.PageSize   = e.PageSize;
            }

            if (this.autoSorting)
            {
                if (e.SortItems != null)
                {
                    foreach (esDataSourceSortItem sortItem in e.SortItems)
                    {
                        esColumnMetadata col = e.Collection.es.Meta.Columns.FindByPropertyName(sortItem.Property);
                        if (col != null)
                        {
                            query.OrderBy(col.Name, sortItem.Direction);
                        }
                        else if (sortItem.Property[0] == '<')
                        {
                            query.OrderBy(sortItem.Property, sortItem.Direction);
                        }
                    }
                }
                else
                {
                    if (this.AutoPaging)
                    {
                        List <esColumnMetadata> pks = e.Collection.es.Meta.Columns.PrimaryKeys;
                        if (pks != null)
                        {
                            foreach (esColumnMetadata pk in pks)
                            {
                                query.OrderBy(pk.Name, esOrderByDirection.Ascending);
                            }
                        }
                    }
                }
            }

            if (this.autoSorting || this.AutoPaging)
            {
                if (e.Query != null)
                {
                    IEntityCollection iColl = e.Collection as IEntityCollection;
                    iColl.HookupQuery(query);
                }

                query.Load();
            }
        }
        private void FetchTotalRowCount(esDataSourceSelectEventArgs e)
        {
            esDynamicQuery query = e.Query != null ? e.Query : e.Collection.es.Query;

            IDynamicQuerySerializableInternal iQuery = query as IDynamicQuerySerializableInternal;

            if (e.Arguments.RetrieveTotalRowCount)
            {
                if (this.totalRowCount == -1)
                {
                    #region Backup
                    //
                    // Back up everything cause we're going to restore it
                    //
                    List <esExpression>  select  = iQuery.InternalSelectColumns;
                    List <esOrderByItem> orderBy = iQuery.InternalOrderByItems;

                    bool   countAll      = query.es.CountAll;
                    string countAllAlias = query.es.CountAllAlias;
                    int?   pageNumber    = query.es.PageNumber;
                    int?   pageSize      = query.es.PageSize;
                    string lastQuery     = query.es.LastQuery;

                    EntitySpaces.Interfaces.esDynamicQuery.QueryLoadedDelegate origDelegate = query.OnLoadDelegate;
                    #endregion

                    //
                    // Clear some stuff so we can make our query
                    //
                    iQuery.InternalSelectColumns = null;
                    iQuery.InternalOrderByItems  = null;

                    query.es.CountAll      = true;
                    query.es.CountAllAlias = "Count";
                    query.es.PageNumber    = null;
                    query.es.PageSize      = null;

                    object o = query.OnLoadDelegate;

                    try
                    {
                        query.OnLoadDelegate = this.OnQueryLoaded;

                        if (query.Load())
                        {
                            this.totalRowCount = Convert.ToInt32(table.Rows[0]["Count"]);
                        }
                    }
                    finally
                    {
                        #region Restore
                        iQuery.InternalSelectColumns = select;
                        iQuery.InternalOrderByItems  = orderBy;

                        query.es.CountAll      = countAll;
                        query.es.CountAllAlias = countAllAlias;
                        query.es.PageNumber    = pageNumber;
                        query.es.PageSize      = pageSize;

                        query.OnLoadDelegate = origDelegate;
                        iQuery.LastQuery     = lastQuery;

                        #endregion Restore
                    }
                }

                e.Arguments.TotalRowCount = this.totalRowCount;
            }
        }