private IEnumerable GetServerData(GridCommand command)
        {
            DataLoadOptions loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Order>(o => o.Customer);

            var dataContext = new NorthwindDataContext
            {
                LoadOptions = loadOptions
            };

            IQueryable<Order> data = dataContext.Orders;

            //Apply filtering
            data = data.ApplyFiltering(command.FilterDescriptors);

            ViewData["Total"] = data.Count();

            //Apply sorting
            data = data.ApplySorting(command.GroupDescriptors, command.SortDescriptors);

            //Apply paging
            data = data.ApplyPaging(command.Page, command.PageSize);

            //Apply grouping
            if (command.GroupDescriptors.Any())
            {
                return data.ApplyGrouping(command.GroupDescriptors);
            }
            return data.ToList();
        }
        public ActionResult _CustomBinding(GridCommand command)
        {
            IEnumerable<Order> data = GetData(command);

            return View(new GridModel
                            {
                                Data = data,
                                Total = data.Count()
                            });
        }
 private bool IsEmptyCommand(GridCommand command)
 {
     return command.PageSize == 0;
 }
 public ActionResult CustomServerBinding(GridCommand command)
 {
     IEnumerable data = GetServerData(!IsEmptyCommand(command) ? command : new GridCommand());
     return View(data);
 }
        private static IEnumerable<Order> GetData(GridCommand command)
        {
            DataLoadOptions loadOptions = new DataLoadOptions();
            loadOptions.LoadWith<Order>(o => o.Customer);

            var dataContext = new NorthwindDataContext
            {
                LoadOptions = loadOptions
            };

            IQueryable<Order> data = dataContext.Orders;

            //Apply filtering
            if (command.FilterDescriptors.Any())
            {
                data = data.Where(ExpressionBuilder.Expression<Order>(command.FilterDescriptors));
            }

            // Apply sorting
            foreach (SortDescriptor sortDescriptor in command.SortDescriptors)
            {
                if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                {
                    switch (sortDescriptor.Member)
                    {
                        case "OrderID":
                            data = data.OrderBy(ExpressionBuilder.Expression<Order, int>(sortDescriptor.Member));
                            break;
                        case "Customer.ContactName":
                            data = data.OrderBy(order => order.Customer.ContactName);
                            break;
                        case "ShipAddress":
                            data = data.OrderBy(order => order.ShipAddress);
                            break;
                        case "OrderDate":
                            data = data.OrderBy(order => order.OrderDate);
                            break;
                    }
                }
                else
                {
                    switch (sortDescriptor.Member)
                    {
                        case "OrderID":
                            data = data.OrderByDescending(order => order.OrderID);
                            break;
                        case "Customer.ContactName":
                            data = data.OrderByDescending(order => order.Customer.ContactName);
                            break;
                        case "ShipAddress":
                            data = data.OrderByDescending(order => order.ShipAddress);
                            break;
                        case "OrderDate":
                            data = data.OrderByDescending(order => order.OrderDate);
                            break;
                    }
                }
            }

            // ... and paging
            if (command.PageSize > 0)
            {
                data = data.Skip((command.Page - 1) * command.PageSize);
            }

            data = data.Take(command.PageSize);

            return data;
        }