public List <OrderSnapshot> FindByCriteriaPaged(OrderSearchCriteria criteria, int pageNumber, int pageSize,
                                                        ref int rowCount)
        {
            using (var strategy = CreateStrategy())
            {
                var query = strategy.GetQuery()
                            .Where(y => y.StoreId == Context.CurrentStore.Id)
                            .Where(y => y.TimeOfOrder >= criteria.StartDateUtc && y.TimeOfOrder <= criteria.EndDateUtc);

                //if (criteria.OrderNumber != string.Empty)
                //{
                //	query = query.Where(y => y.OrderNumber == criteria.OrderNumber);
                //}

                if (!criteria.IsIncludeCanceledOrder)
                {
                    // Order is not Cancelled
                    query = query.Where(y => y.StatusCode != OrderStatusCode.Cancelled);
                }

                // Order Number
                if (!string.IsNullOrEmpty(criteria.OrderNumber))
                {
                    query = query.Where(y => y.OrderNumber == criteria.OrderNumber);
                }

                // Is Recurring
                if (criteria.IsRecurring.HasValue)
                {
                    query = query.Where(y => y.IsRecurring == criteria.IsRecurring.Value);
                }

                // Status Code
                if (!string.IsNullOrEmpty(criteria.StatusCode))
                {
                    query = query.Where(y => y.StatusCode == criteria.StatusCode);
                }

                // Affiliate Id
                if (criteria.AffiliateId.HasValue)
                {
                    query = query.Where(y => y.AffiliateId == criteria.AffiliateId);
                }

                // Payment Status
                if (criteria.PaymentStatus != OrderPaymentStatus.Unknown)
                {
                    var tempPay = (int)criteria.PaymentStatus;
                    query = query.Where(y => y.PaymentStatus == tempPay);

                    //If payment is done then include orders even if they are placed or not
                    if (criteria.PaymentStatus == OrderPaymentStatus.Paid || criteria.PaymentStatus == OrderPaymentStatus.PartiallyPaid || criteria.PaymentStatus == OrderPaymentStatus.Overpaid)
                    {
                        criteria.IncludeUnplaced = true;
                    }
                }

                // Shipping Status
                if (criteria.ShippingStatus != OrderShippingStatus.Unknown)
                {
                    var tempShip = (int)criteria.ShippingStatus;
                    query = query.Where(y => y.ShippingStatus == tempShip);
                }

                // Is Placed
                if (!criteria.IncludeUnplaced)
                {
                    query = query.Where(y => y.IsPlaced == (criteria.IsPlaced ? 1 : 0) || string.IsNullOrEmpty(y.OrderNumber) == false);
                }

                // Keyword (most expensive operation)
                if (!string.IsNullOrEmpty(criteria.Keyword))
                {
                    var orderNumber = 0;
                    var firstChar   = 0;
                    if (int.TryParse(criteria.Keyword, out orderNumber))
                    {
                        query = query.Where(y => y.OrderNumber.Contains(criteria.Keyword));
                    }
                    else if (criteria.Keyword.IndexOf("@") >= 0)
                    {
                        query = query.Where(y => y.UserEmail.Contains(criteria.Keyword));
                    }
                    else if (int.TryParse(criteria.Keyword.Substring(0, 1), out firstChar))
                    {
                        query = query.Where(y => y.BillingAddress.Contains(criteria.Keyword) ||
                                            y.ShippingAddress.Contains(criteria.Keyword));
                    }
                    else
                    {
                        query = query.Where(y => y.UserEmail.Contains(criteria.Keyword) ||
                                            y.BillingAddress.Contains(criteria.Keyword) ||
                                            y.ShippingAddress.Contains(criteria.Keyword));
                    }

                    //
                    // TODO:
                    // From Will:
                    // We need to populate the return results properly with all orders that partially match a product name.
                    // Probably need to add a new "LineItemSnapshot" property & class to OrderSnapshot to make this feasible.
                    // The code below was a POC that clearly won't work, and if fixed, might not be performant enough.
                    //

                    // get a collection of line items for the remaining filtered orders
                    //var lineItems = itemRepository.FindForOrders(items.Select(y => y.bvin).ToList());

                    // find all of the orders that contain the product name
                    //List<string> orderIds =
                    //  (from li in lineItems where li.ProductName.Contains(criteria.Keyword) select li.OrderBvin).Distinct()
                    //      .ToList();

                    // merge the results with the filtered order items
                    // (code never written)
                }

                // return total item count;
                rowCount = query.Count();

                if (criteria.SortDescending)
                {
                    query = query.OrderByDescending(y => y.TimeOfOrder);
                }
                else
                {
                    query = query.OrderBy(y => y.TimeOfOrder);
                }

                var pagedQuery = GetPagedItems(query, pageNumber, pageSize);
                return(ListPocoSnapshot(pagedQuery));
            }
        }
        public int CountByCriteria(OrderSearchCriteria criteria)
        {
            using (var strategy = CreateStrategy())
            {
                var query = strategy.GetQuery()
                            .Where(y => y.StoreId == Context.CurrentStore.Id)
                            .Where(y => y.TimeOfOrder >= criteria.StartDateUtc && y.TimeOfOrder <= criteria.EndDateUtc);

                // Order Number
                if (criteria.OrderNumber != string.Empty)
                {
                    query = query.Where(y => y.OrderNumber == criteria.OrderNumber);
                }

                // Is Placed
                query = query.Where(y => y.IsPlaced == (criteria.IsPlaced ? 1 : 0));

                // Is Recurring
                if (criteria.IsRecurring.HasValue)
                {
                    query = query.Where(y => y.IsRecurring == criteria.IsRecurring.Value);
                }

                // Status Code
                if (!string.IsNullOrEmpty(criteria.StatusCode))
                {
                    query = query.Where(y => y.StatusCode == criteria.StatusCode);
                }

                // Affiliate Id
                if (criteria.AffiliateId.HasValue)
                {
                    query = query.Where(y => y.AffiliateId == criteria.AffiliateId);
                }

                // Payment Status
                if (criteria.PaymentStatus != OrderPaymentStatus.Unknown)
                {
                    var tempPay = (int)criteria.PaymentStatus;
                    query = query.Where(y => y.PaymentStatus == tempPay);
                }

                // Shipping Status
                if (criteria.ShippingStatus != OrderShippingStatus.Unknown)
                {
                    var tempShip = (int)criteria.ShippingStatus;
                    query = query.Where(y => y.ShippingStatus == tempShip);
                }

                // Keyword (most expensive operation)
                if (criteria.Keyword != string.Empty)
                {
                    query = query.Where(y => y.OrderNumber.Contains(criteria.Keyword) ||
                                        y.UserEmail.Contains(criteria.Keyword) ||
                                        y.BillingAddress.Contains(criteria.Keyword) ||
                                        y.ShippingAddress.Contains(criteria.Keyword));
                }

                // return total item count;
                return(query.Count());
            }
        }
        public List <OrderSnapshot> FindByCriteria(OrderSearchCriteria criteria)
        {
            var temp = -1;

            return(FindByCriteriaPaged(criteria, 1, int.MaxValue, ref temp));
        }