示例#1
0
        public ActionResult Orders_Read([DataSourceRequest] DataSourceRequest request)
        {
            IQueryable <Order> orders = new NorthwindEntities().Orders;

            if (request.Sorts.Any())
            {
                foreach (SortDescriptor sortDescriptor in request.Sorts)
                {
                    if (sortDescriptor.SortDirection == ListSortDirection.Ascending)
                    {
                        switch (sortDescriptor.Member)
                        {
                        case "OrderID":
                            orders = orders.OrderBy(order => order.OrderID);
                            break;

                        case "ShipAddress":
                            orders = orders.OrderBy(order => order.ShipAddress);
                            break;
                        }
                    }
                    else
                    {
                        switch (sortDescriptor.Member)
                        {
                        case "OrderID":
                            orders = orders.OrderByDescending(order => order.OrderID);
                            break;

                        case "ShipAddress":
                            orders = orders.OrderByDescending(order => order.ShipAddress);
                            break;
                        }
                    }
                }
            }
            else
            {
                // EF can't page unsorted data
                orders = orders.OrderBy(o => o.OrderID);
            }

            var total = orders.Count();

            // Apply paging
            if (request.Page > 0)
            {
                orders = orders.Skip((request.Page - 1) * request.PageSize);
            }
            orders = orders.Take(request.PageSize);

            var result = new DataSourceResult()
            {
                Data  = orders, // Process data (paging and sorting applied)
                Total = total   // Total number of records
            };

            return(Json(result));
        }