Ejemplo n.º 1
0
        /// <summary>
        /// Implements a quasi-repository for searching orders
        /// </summary>
        /// <param name="criteria"></param>
        /// <returns></returns>
        public IList<OrderSearchResult> GetOrders(OrderSearchCriteria criteria)
        {
            using (var context = new HandledNSTEntities())
            {
                IQueryable<Order> matches = context.Orders
                    .Include("Customer")
                    .Include("OrderDetails")
                    .Include("OrderDetails.Product");

                if (!string.IsNullOrWhiteSpace(criteria.OrderNumber.Trim()))
                {
                    matches = matches.Where(x => x.Number.Equals(criteria.OrderNumber));
                }

                if (!string.IsNullOrWhiteSpace(criteria.SKU.Trim()))
                {
                    matches = matches.Where(x => x.OrderDetails.Any(y => y.Product.SKU.Equals(criteria.SKU)));
                }

                if (!string.IsNullOrWhiteSpace(criteria.FirstName.Trim()))
                {
                    matches = matches.Where(x => x.Customer.FirstName.Contains(criteria.FirstName));
                }

                if (!string.IsNullOrWhiteSpace(criteria.LastName.Trim()))
                {
                    matches = matches.Where(x => x.Customer.LastName.Contains(criteria.LastName));
                }

                if (criteria.OrderStartDate.HasValue)
                {
                    matches = matches.Where(x => x.OrderDate >= criteria.OrderStartDate);
                }

                if (criteria.OrderEndDate.HasValue)
                {
                    matches = matches.Where(x => x.OrderDate <= criteria.OrderEndDate);
                }

                // The following call to .ToList() is what actually executes the query against
                // the database.
                return (from entity in matches.ToList()
                        select new OrderSearchResult
                                   {
                                       Id = entity.Id,
                                       OrderNumber = entity.Number,
                                       OrderDate = entity.OrderDate,
                                       FirstName = entity.Customer.FirstName,
                                       LastName = entity.Customer.LastName
                                   }).ToList();

            }
        }
Ejemplo n.º 2
0
        public ActionResult GetOrders(string firstName, string lastName, DateTime? startDate, DateTime? endDate, string orderNumber, string SKU)
        {
            var criteria = new OrderSearchCriteria
                               {
                                   FirstName = firstName,
                                   LastName = lastName,
                                   OrderStartDate = startDate,
                                   OrderEndDate = endDate,
                                   OrderNumber = orderNumber,
                                   SKU = SKU
                               };

            var orders = DataAccess.GetOrders(criteria);

            return View("Index", new GridModel<OrderSearchResult> {Data = orders});
        }