public virtual GenericSearchResult <DerivativeContractItem> SearchDerivativeContractItems(DerivativeContractItemSearchCriteria criteria)
        {
            using (var repository = _repositoryFactory())
            {
                repository.DisableChangesTracking();

                var query = repository.DerivativeContractItems;

                if (!criteria.DerivativeContractIds.IsNullOrEmpty())
                {
                    query = query.Where(dci => criteria.DerivativeContractIds.Contains(dci.DerivativeContractId));
                }

                if (!criteria.MemberIds.IsNullOrEmpty())
                {
                    query = query.Where(dci => criteria.MemberIds.Contains(dci.DerivativeContract.MemberId));
                }

                if (!criteria.FulfillmentCenterIds.IsNullOrEmpty())
                {
                    query = query.Where(dci => criteria.FulfillmentCenterIds.Contains(dci.FulfillmentCenterId));
                }

                if (!criteria.ProductIds.IsNullOrEmpty())
                {
                    query = query.Where(dci => criteria.ProductIds.Contains(dci.ProductId));
                }

                if (!criteria.Types.IsNullOrEmpty())
                {
                    var criteriaTypes = criteria.Types.Select(x => x.ToString()).ToArray();
                    query = query.Where(dci => criteriaTypes.Contains(dci.DerivativeContract.Type));
                }

                if (criteria.OnlyActive)
                {
                    var now = DateTime.Now;
                    query = query.Where(dci => dci.DerivativeContract.IsActive && dci.DerivativeContract.StartDate <= now && (dci.DerivativeContract.EndDate == null || dci.DerivativeContract.EndDate >= now) && dci.RemainingQuantity > 0);
                }

                if (criteria.OnlyRemaining)
                {
                    query = query.Where(dci => dci.DerivativeContract.IsActive && dci.RemainingQuantity > 0);
                }

                var predicate = GetQueryPredicate(criteria);
                query = query.Where(predicate.Expand());

                var sortInfos = criteria.SortInfos;
                if (sortInfos.IsNullOrEmpty())
                {
                    sortInfos = new[]
                    {
                        new SortInfo {
                            SortColumn = "DerivativeContract.StartDate", SortDirection = SortDirection.Descending
                        },
                        new SortInfo {
                            SortColumn = "DerivativeContract.EndDate", SortDirection = SortDirection.Ascending
                        }
                    };
                }
                query = query.OrderBySortInfos(sortInfos);

                var totalCount            = query.Count();
                var derivativeContractIds = query.Select(dc => dc.Id).Skip(criteria.Skip).Take(criteria.Take).ToArray();

                return(new GenericSearchResult <DerivativeContractItem>
                {
                    Results = _derivativeContractService.GetDerivativeContractItemsByIds(derivativeContractIds).OrderBy(dc => dc.Id).ToList(),
                    TotalCount = totalCount
                });
            }
        }
        public IHttpActionResult SearchItems(DerivativeContractItemSearchCriteria criteria)
        {
            var result = _derivativeContractSearchService.SearchDerivativeContractItems(criteria);

            return(Ok(result));
        }
 /// <summary>
 /// Used to define extra where clause for derivative contract items search
 /// </summary>
 /// <param name="criteria"></param>
 /// <returns></returns>
 protected virtual Expression <Func <DerivativeContractItemEntity, bool> > GetQueryPredicate(DerivativeContractItemSearchCriteria criteria)
 {
     if (!criteria.StartDateRanges.IsNullOrEmpty() || !criteria.EndDateRanges.IsNullOrEmpty())
     {
         var predicate = PredicateBuilder.False <DerivativeContractItemEntity>();
         if (!criteria.StartDateRanges.IsNullOrEmpty())
         {
             predicate = PredicateBuilder.Or(predicate, ((Expression <Func <DerivativeContractItemEntity, DateTime?> >)(dci => dci.DerivativeContract.StartDate)).IsInRanges(criteria.StartDateRanges));
         }
         if (!criteria.EndDateRanges.IsNullOrEmpty())
         {
             predicate = PredicateBuilder.Or(predicate, ((Expression <Func <DerivativeContractItemEntity, DateTime?> >)(dci => dci.DerivativeContract.EndDate)).IsInRanges(criteria.EndDateRanges));
         }
         return(predicate.Expand());
     }
     return(PredicateBuilder.True <DerivativeContractItemEntity>());
 }