public static void EFPlus_FutureQuery() { CUI.MainHeadline(nameof(EFPlus_FutureQuery)); using (var ctx = new DA.WWWingsContext()) { ctx.Log(); CUI.Headline("Define three future queries ... nothing happens in the database"); QueryFutureEnumerable <Pilot> qAllePilots = ctx.PilotSet.Future(); QueryFutureEnumerable <Flight> qflightSetRome = ctx.FlightSet.Where(x => x.Departure == "Rome").Future(); QueryFutureEnumerable <Flight> qFlightSetBerlin = ctx.FlightSet.Where(x => x.Departure == "Berlin").Future(); CUI.Headline("Access the pilots:"); var allePilots = qAllePilots.ToList(); Console.WriteLine(allePilots.Count + " Pilots are loaded!"); CUI.Headline("Access the flights from Rome:"); var flightSetRom = qflightSetRome.ToList(); Console.WriteLine(flightSetRom.Count + " flights from Berlin are loaded!"); CUI.Headline("Define another two future queries ... nothing happens in the database"); QueryFutureEnumerable <Flight> qFugSetLondon = ctx.FlightSet.Where(x => x.Departure == "London").Future(); QueryFutureEnumerable <Flight> qflightSetParis = ctx.FlightSet.Where(x => x.Departure == "Paris").Future(); CUI.Headline("Access the flights from Berlin:"); var flightSetBerlin = qFlightSetBerlin.ToList(); Console.WriteLine(flightSetBerlin.Count + " flights from Rome are loaded!"); CUI.Headline("Access the flights from London:"); var flightSetLondon = qFugSetLondon.ToList(); Console.WriteLine(flightSetLondon.Count + " flights from London are loaded!"); CUI.Headline("Access the flights from Paris:"); var flightSetParis = qflightSetParis.ToList(); Console.WriteLine(flightSetParis.Count + " flights from Paris are loaded!"); } }
public static async Task <T> SingleOrDefaultAsync <T>(this QueryFutureEnumerable <T> source) { if (!source.HasValue) { await source.OwnerBatch.ExecuteQueriesAsync().ConfigureAwait(false); } using (var enumerator = source.GetEnumerator()) { if (!enumerator.MoveNext()) { return(default(T)); } var current = enumerator.Current; if (!enumerator.MoveNext()) { return(current); } } throw new InvalidOperationException("Sequence contains more than one element"); }
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(); 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()); }
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()); }
public static Task <T> ValueAsync <T>(this QueryFutureEnumerable <T> source) where T : struct { return(source.SingleOrDefaultAsync()); }