public IEnumerable <ICategoryWithProductDto> GetCategoriesWithProducts(Guid categoryRootId, Guid?brandId)
        {
            var categories = _categoryRepository.AsQuery().Where(item => item.Id == categoryRootId)
                             .SelectMany(item => item.SubCategories).Where(item => item.IsActive);
            var dto = new List <ICategoryWithProductDto>();

            foreach (var category in categories.ToList())
            {
                var temp = new CategoryWithProductDto
                {
                    Id            = category.Id,
                    IsActive      = category.IsActive,
                    Name          = category.Name,
                    IsRoot        = true,
                    CategoryImage = new CategoryImageDto
                    {
                        TopPageCatImage  = category.CategoryImage.TopPageCatImage,
                        FullMainCatImage = category.CategoryImage.FullMainCatImage,
                        MainCatImage     = category.CategoryImage.MainCatImage
                    },
                    Description = category.Description,
                    Order       = category.Order
                };
                var products = _productRepository.AsQuery()
                               .Where(item => item.IsActive && item.Category.Id == category.Id)
                               .WhereIf(brandId != null, item => item.Brand.Id == brandId)
                               .OrderByDescending(item => item.CreationTime).Take(20).ToList();
                temp.Products = products.Select(item => item.ToProductDto()).ToList();
                if (temp.Products.Any())
                {
                    dto.Add(temp);
                }
            }
            return(dto.OrderBy(p => p.Order));
        }
        public IEnumerable <ICategoryWithProductDto> GetCategoryRootsWithProducts()
        {
            var categoryRoots = _categoryRepository.AsQuery().OfType <CategoryRoot>()
                                .Where(item => item.IsActive).ToList();
            var dto = new List <ICategoryWithProductDto>();

            foreach (var categoryRoot in categoryRoots)
            {
                var temp = new CategoryWithProductDto
                {
                    Id            = categoryRoot.Id,
                    IsActive      = categoryRoot.IsActive,
                    Name          = categoryRoot.Name,
                    IsRoot        = true,
                    CategoryImage = new CategoryImageDto
                    {
                        TopPageCatImage  = categoryRoot.CategoryImage.TopPageCatImage,
                        FullMainCatImage = categoryRoot.CategoryImage.FullMainCatImage,
                        MainCatImage     = categoryRoot.CategoryImage.MainCatImage
                    },
                    Description = categoryRoot.Description,
                    Order       = categoryRoot.Order
                };
                var products      = _productRepository.AsQuery().Where(item => item.IsActive);
                var subCategories = _categoryRepository.AsQuery().Where(item => item.Id == categoryRoot.Id)
                                    .SelectMany(i => i.SubCategories).Where(item => item.IsActive);
                var categoryRootProducts = _productRepository.AsQuery().Where(item => item.IsActive && item.Category.Id == categoryRoot.Id);
                var subCategoryProducts  = products.Join(subCategories, p => p.Category.Id, s => s.Id,
                                                         (pro, cat) => new
                {
                    Product = pro,
                }).Select(item => item.Product);
                var union = categoryRootProducts.Union(subCategoryProducts)
                            .OrderByDescending(item => item.CreationTime).Take(20).ToList();
                temp.Products = union.Select(item => item.ToProductDto()).ToList();
                if (temp.Products.Any())
                {
                    dto.Add(temp);
                }
            }
            return(dto.OrderBy(p => p.Order));
        }