コード例 #1
0
        public GenericSearchResult <PropertyDictionaryItem> Search(PropertyDictionaryItemSearchCriteria searchCriteria)
        {
            if (searchCriteria == null)
            {
                throw new ArgumentNullException(nameof(searchCriteria));
            }

            return(_cacheManager.Get($"PropertyDictionaryItemService.Search-{searchCriteria.GetCacheKey()}", CatalogConstants.DictionaryItemsCacheRegion, () =>
            {
                using (var repository = _repositoryFactory())
                {
                    //Optimize performance and CPU usage
                    repository.DisableChangesTracking();

                    var result = new GenericSearchResult <PropertyDictionaryItem>();

                    var query = repository.PropertyDictionaryItems;
                    if (!searchCriteria.CatalogIds.IsNullOrEmpty())
                    {
                        query = query.Where(x => searchCriteria.CatalogIds.Contains(x.Property.CatalogId));
                    }
                    if (!searchCriteria.PropertyIds.IsNullOrEmpty())
                    {
                        query = query.Where(x => searchCriteria.PropertyIds.Contains(x.PropertyId));
                    }
                    if (!string.IsNullOrEmpty(searchCriteria.SearchPhrase))
                    {
                        query = query.Where(x => x.Alias.Contains(searchCriteria.SearchPhrase));
                    }

                    var sortInfos = searchCriteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] {
                            new SortInfo {
                                SortColumn = "SortOrder", SortDirection = SortDirection.Ascending
                            },
                            new SortInfo {
                                SortColumn = "Alias", SortDirection = SortDirection.Ascending
                            }
                        };
                    }
                    //Force sorting order by Id to prevent issue with nondeterministic behavior for queries that contains pagination with data that have same values for sorting by columns
                    //https://github.com/VirtoCommerce/vc-platform/issues/1525
                    query = query.OrderBySortInfos(sortInfos).ThenBy(x => x.Id);

                    result.TotalCount = query.Count();
                    var ids = query.Skip(searchCriteria.Skip).Take(searchCriteria.Take).Select(x => x.Id).ToArray();
                    result.Results = GetByIds(ids).AsQueryable().OrderBySortInfos(sortInfos).ToList();
                    return result;
                }
            }));
        }
コード例 #2
0
        public GenericSearchResult <PropertyDictionaryItem> Search(PropertyDictionaryItemSearchCriteria criteria)
        {
            if (criteria == null)
            {
                throw new ArgumentNullException(nameof(criteria));
            }

            return(_cacheManager.Get($"PropertyDictionaryItemService.Search-{criteria.GetCacheKey()}", CatalogConstants.DictionaryItemsCacheRegion, () =>
            {
                using (var repository = _repositoryFactory())
                {
                    //Optimize performance and CPU usage
                    repository.DisableChangesTracking();

                    var result = new GenericSearchResult <PropertyDictionaryItem>();

                    var query = repository.PropertyDictionaryItems;
                    if (!criteria.PropertyIds.IsNullOrEmpty())
                    {
                        query = query.Where(x => criteria.PropertyIds.Contains(x.PropertyId));
                    }
                    if (!string.IsNullOrEmpty(criteria.SearchPhrase))
                    {
                        query = query.Where(x => x.Alias.Contains(criteria.SearchPhrase));
                    }

                    var sortInfos = criteria.SortInfos;
                    if (sortInfos.IsNullOrEmpty())
                    {
                        sortInfos = new[] {
                            new SortInfo {
                                SortColumn = "SortOrder", SortDirection = SortDirection.Ascending
                            },
                            new SortInfo {
                                SortColumn = "Alias", SortDirection = SortDirection.Ascending
                            }
                        };
                    }

                    query = query.OrderBySortInfos(sortInfos);

                    result.TotalCount = query.Count();
                    var ids = query.Skip(criteria.Skip).Take(criteria.Take).Select(x => x.Id).ToArray();
                    result.Results = GetByIds(ids).AsQueryable().OrderBySortInfos(sortInfos).ToList();
                    return result;
                }
            }));
        }