Esempio n. 1
0
        public async Task <IActionResult> IndexAsync(CancellationToken cancellationToken = default)
        {
            await Context.Database.EnsureCreatedAsync(cancellationToken);

            await Context.GenerateDataAsync(cancellationToken);


            int count = await Context.Customers.CountAsync(cancellationToken);

            int futureCount = Context.Customers.Future().Count();

            Console.WriteLine("Future Customer Count: {0}", futureCount);


            QueryFutureValue <int> futureMaxPrice =
                Context.Products.DeferredMax(x => x.Prices).FutureValue <int>();

            QueryFutureValue <int> futureMinPrice =
                Context.Products.DeferredMin(x => x.Prices).FutureValue <int>();

            int maxPrice = futureMaxPrice.Value;
            int minPrice = futureMinPrice.Value;

            Console.WriteLine("Max Price: {0}", maxPrice);
            Console.WriteLine("Min Price: {0}", minPrice);


            IEnumerable <Product> resultPeopleActive =
                await Context.Products.AsNoTracking()
                .Where(c => c.IsActive)
                .ToListAsync(cancellationToken)
            ;

            IEnumerable <Product> resultPeopleNonActive =
                await Context.Products.AsNoTracking()
                .Where(c => !c.IsActive)
                .ToListAsync(cancellationToken)
            ;

            IEnumerable <Product> resultPeopleActiveWithFuture =
                await Context.Products.AsNoTracking()
                .Where(c => c.IsActive)
                .Future().ToListAsync(cancellationToken)
            ;

            IEnumerable <Product> resultPeopleNonActiveWithFuture =
                await Context.Products.AsNoTracking()
                .Where(c => !c.IsActive)
                .Future().ToListAsync(cancellationToken)
            ;

            if (resultPeopleActive == resultPeopleActiveWithFuture &&
                resultPeopleNonActive == resultPeopleNonActiveWithFuture)
            {
                Console.WriteLine("Results are the same .. !!!!");
            }

            IQueryable <Customer>            baseQuery           = Context.Customers.Where(c => c.IsActive);
            QueryFutureEnumerable <Customer> getTotalCountFuture = baseQuery.Future();
            QueryFutureEnumerable <Customer> getPageFuture       = baseQuery.Skip(1).Take(4).Future();
            int getTotalCountResult = getTotalCountFuture.Count();
            IEnumerable <Customer> getPageResult = await getPageFuture.ToListAsync(cancellationToken);

            return(View());
        }
Esempio n. 2
0
        public async Task <IActionResult> IndexAsync(CancellationToken cancellationToken = default)
        {
            await Context.Database.EnsureCreatedAsync(cancellationToken);

            await Context.GenerateDataAsync(cancellationToken);


            int count = await Context.Customers.AsNoTracking().CountAsync(cancellationToken);

            int futureCount = Context.Customers.AsNoTracking().Future().Count();

            _logger.LogInformation($"Future Customer Count: {futureCount}");


            QueryFutureValue <int> futureMaxPrice =
                Context.Products.AsNoTracking().DeferredMax(x => x.Prices).FutureValue <int>();

            QueryFutureValue <int> futureMinPrice =
                Context.Products.AsNoTracking().DeferredMin(x => x.Prices).FutureValue <int>();

            int maxPrice = futureMaxPrice.Value;
            int minPrice = futureMinPrice.Value;

            _logger.LogInformation($"Max Price: {maxPrice}");
            _logger.LogInformation($"Min Price: {minPrice}");


            IEnumerable <Product> resultPeopleActive =
                await Context.Products.AsNoTracking()
                .Where(c => c.IsActive)
                .ToListAsync(cancellationToken)
            ;

            IEnumerable <Product> resultPeopleNonActive =
                await Context.Products.AsNoTracking()
                .Where(c => !c.IsActive)
                .ToListAsync(cancellationToken)
            ;

            QueryFutureEnumerable <Product> resultPeopleActiveWithFuture =
                Context.Products.AsNoTracking()
                .Where(c => c.IsActive)
                .Future()
            ;
            QueryFutureEnumerable <Product> resultPeopleNonActiveWithFuture =
                Context.Products.AsNoTracking()
                .Where(c => !c.IsActive)
                .Future()
            ;

            IEnumerable <Product> resultPeopleActiveWith =
                await resultPeopleActiveWithFuture.ToListAsync(cancellationToken);

            IEnumerable <Product> resultPeopleNonActiveWith =
                await resultPeopleNonActiveWithFuture.ToListAsync(cancellationToken);

            if (resultPeopleActive == resultPeopleActiveWith &&
                resultPeopleNonActive == resultPeopleNonActiveWith)
            {
                _logger.LogInformation("Results are the same .. !!!!");
            }


            IQueryable <Product> query = Context.Products.AsNoTracking().Where(c => c.IsActive);

            var pagedQueryable = await query
                                 .OrderBy(p => p.ProductId)
                                 .Skip(1).Take(4)
                                 .Select(p => new
            {
                TotalAsync = query.CountAsync(cancellationToken),
                Item       = p
            })
                                 .ToListAsync(cancellationToken)
            ;

            int totalQuery = await pagedQueryable.FirstOrDefault().TotalAsync;

            IEnumerable <Product> items = pagedQueryable.Select(p => p.Item).ToList();

            var page = await query
                       .OrderBy(p => p.Name)
                       .Select(p => p)
                       .Skip(1).Take(4)
                       .GroupBy(p => new { TotalAsync = query.CountAsync(cancellationToken) })
                       .FirstOrDefaultAsync(cancellationToken)
            ;

            int total = await page.Key.TotalAsync;
            IEnumerable <Product> people = page.Select(p => p);

            IEnumerable <ProductResult> results = await query
                                                  .OrderBy(p => p.Name)
                                                  .Skip(1).Take(4)
                                                  .Select(p => new ProductResult(p, query.Count()))
                                                  .ToListAsync(cancellationToken)
            ;

            int totalCount = results.FirstOrDefault().TotalCount;
            IEnumerable <ProductResult> peopleResult = results.Select(p => p).ToList();

            IQueryable <Product>   queryAsQueryable = Context.Products.AsNoTracking().AsQueryable();
            Task <List <Product> > resultsTask      =
                query.OrderBy(p => p.ProductId).Skip(1).Take(4).ToListAsync(cancellationToken);
            Task <int> countTask = query.CountAsync(cancellationToken);
            await Task.WhenAll(resultsTask, countTask);

            int TotaslCount            = await countTask;
            IEnumerable <Product> Data = await resultsTask;

            IQueryable <Customer>            baseQuery           = Context.Customers.Where(c => c.IsActive);
            QueryFutureEnumerable <Customer> getTotalCountFuture = baseQuery.Future();
            QueryFutureEnumerable <Customer> getPageFuture       = baseQuery.Skip(1).Take(4).Future();
            int getTotalCountResult = getTotalCountFuture.Count();
            IEnumerable <Customer> getPageResult = await getPageFuture.ToListAsync(cancellationToken);

            return(View());
        }
        public async Task <IActionResult> IndexAsync(CancellationToken cancellationToken = default)
        {
            await Context.Database.EnsureCreatedAsync(cancellationToken);

            await Context.GenerateDataAsync(cancellationToken);


            int count = await Context.Customers.CountAsync(cancellationToken);

            int futureCount = Context.Customers.Future().Count();

            _logger.LogInformation($"Future Customer Count: {futureCount}");


            QueryFutureValue <int> futureMaxPrice =
                Context.Products.DeferredMax(x => x.Prices).FutureValue <int>();

            QueryFutureValue <int> futureMinPrice =
                Context.Products.DeferredMin(x => x.Prices).FutureValue <int>();

            int maxPrice = futureMaxPrice.Value;
            int minPrice = futureMinPrice.Value;

            _logger.LogInformation($"Max Price: {maxPrice}");
            _logger.LogInformation($"Min Price: {minPrice}");


            IEnumerable <Product> resultPeopleActive =
                await Context.Products.AsNoTracking()
                .Where(c => c.IsActive)
                .ToListAsync(cancellationToken)
            ;

            IEnumerable <Product> resultPeopleNonActive =
                await Context.Products.AsNoTracking()
                .Where(c => !c.IsActive)
                .ToListAsync(cancellationToken)
            ;

            QueryFutureEnumerable <Product> resultPeopleActiveWithFuture =
                Context.Products.AsNoTracking()
                .Where(c => c.IsActive)
                .Future()
            ;
            QueryFutureEnumerable <Product> resultPeopleNonActiveWithFuture =
                Context.Products.AsNoTracking()
                .Where(c => !c.IsActive)
                .Future()
            ;

            IEnumerable <Product> resultPeopleActiveWith =
                await resultPeopleActiveWithFuture.ToListAsync(cancellationToken);

            IEnumerable <Product> resultPeopleNonActiveWith =
                await resultPeopleNonActiveWithFuture.ToListAsync(cancellationToken);

            if (resultPeopleActive == resultPeopleActiveWith &&
                resultPeopleNonActive == resultPeopleNonActiveWith)
            {
                _logger.LogInformation("Results are the same .. !!!!");
            }


            IQueryable <Product> query = Context.Products.Where(c => c.IsActive);
            var page = await query
                       .OrderBy(p => p.Name)
                       .Skip(1).Take(4)
                       .GroupBy(p => new { Total = query.Count() })
                       .FirstOrDefaultAsync(cancellationToken);

            int total = page.Key.Total;
            IEnumerable <Product> people = page.Select(p => p);

            var results = query
                          .OrderBy(p => p.Name)
                          .Select(p => new
            {
                Product    = p,
                TotalCount = query.Count()
            })
                          .Skip(1).Take(4)
                          .ToArray();

            var totalCount = results.First().TotalCount;
            var people     = results.Select(r => r.Person).ToArray();


            IQueryable <Customer>            baseQuery           = Context.Customers.Where(c => c.IsActive);
            QueryFutureEnumerable <Customer> getTotalCountFuture = baseQuery.Future();
            QueryFutureEnumerable <Customer> getPageFuture       = baseQuery.Skip(1).Take(4).Future();
            int getTotalCountResult = getTotalCountFuture.Count();
            IEnumerable <Customer> getPageResult = await getPageFuture.ToListAsync(cancellationToken);


            return(View());
        }