public IEnumerable<Order> GetPastOrders(int waiterId, int firstIndex, int lastIndex) { if (!CheckHasUserRole(waiterId, UserRole.Waiter)) throw new SecurityException(String.Format("User id={0} is not logged in or is not a waiter.", waiterId)); if(firstIndex < 0) throw new ArgumentException(String.Format("firstIndex ({0]) is smaller than zero", firstIndex)); if(lastIndex < 0) throw new ArgumentException(String.Format("lastIndex ({0]) is smaller than zero", lastIndex)); if (firstIndex > lastIndex) throw new ArgumentException(String.Format("firstIndex ({0}) is larger than lastIndex ({1})", firstIndex, lastIndex)); using(var db = new DataAccessProvider()) { var sortedList = db.Orders.Include("MenuItems").Include("MenuItems.MenuItem").Include("MenuItems.MenuItem.Category").Include("Waiter").Include("Table").Where(o => o.Waiter.Id == waiterId && !o.IsDeleted && (o.State == OrderState.Realized || o.State == OrderState.NotRealized)).OrderByDescending(o => o.ClosingDate).ToList(); //Kelner obsłużył mniej zamówień niż pierwszy indeks if (sortedList.Count < firstIndex + 1) return new Order[0]; //Indeks końcowy i początkowy taki sam - zwracamy jedną wartość if (firstIndex == lastIndex) return new Order[1] { new Order(sortedList[firstIndex]) }; //Kelner ma mniej zamówień niż indeks końcowy - zwracamy od indeksu początkowego do końca if(sortedList.Count < lastIndex + 1) { var result = new OrderEntity[sortedList.Count - firstIndex]; sortedList.CopyTo(firstIndex, result, 0, sortedList.Count - firstIndex); return result.Select( o => new Order(o)).ToList(); } var resultOrders = new OrderEntity[lastIndex - firstIndex + 1]; sortedList.CopyTo(firstIndex, resultOrders, 0, lastIndex - firstIndex + 1); return resultOrders.Select( o => new Order(o)).ToList(); } }