public void Try_Cast_Null_FutureValue_To_Certain_Type_Should_Return_Null() { QueryFutureValue <string> futureValue = null; string tmp = futureValue; Assert.IsNull(tmp); }
/// <summary> /// Defer the execution of the <paramref name="query" /> and batch the query command with other /// future queries. The batch is executed when a future query requires a database round trip. /// </summary> /// <typeparam name="TResult">The type of the query result.</typeparam> /// <param name="query">The query to defer the execution and to add in the batch of future queries.</param> /// <returns> /// The QueryFutureValue<TResult,TResult> added to the batch of futures queries. /// </returns> public static QueryFutureValue <TResult> FutureValue <TResult>(this QueryDeferred <TResult> query) { if (!QueryFutureManager.AllowQueryBatch) { var futureValue = new QueryFutureValue <TResult>(null, null); futureValue.GetResultDirectly(query.Query); return(futureValue); } #if EF5 || EF6 var objectQuery = query.Query.GetObjectQuery(); var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context); var futureQuery = new QueryFutureValue <TResult>(futureBatch, objectQuery); #elif EFCORE QueryFutureBatch futureBatch; QueryFutureValue <TResult> futureQuery; if (query.Query.IsInMemoryQueryContext()) { var context = query.Query.GetInMemoryContext(); futureBatch = QueryFutureManager.AddOrGetBatch(context); futureBatch.IsInMemory = true; futureQuery = new QueryFutureValue <TResult>(futureBatch, query.Query); futureQuery.InMemoryDeferredQuery = query; } else { var context = query.Query.GetDbContext(); futureBatch = QueryFutureManager.AddOrGetBatch(context); futureQuery = new QueryFutureValue <TResult>(futureBatch, query.Query); } #endif futureBatch.Queries.Add(futureQuery); return(futureQuery); }
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 .. !!!!"); } return(View()); }
/// <summary> /// Defer the execution of the <paramref name="query" /> and batch the query command with other /// future queries. The batch is executed when a future query requires a database round trip. /// </summary> /// <typeparam name="TResult">The type of the query result.</typeparam> /// <param name="query">The query to defer the execution and to add in the batch of future queries.</param> /// <returns> /// The QueryFutureValue<TResult,TResult> added to the batch of futures queries. /// </returns> public static QueryFutureValue <TResult> FutureValue <TResult>(this QueryDeferred <TResult> query) { #if EF5 || EF6 var objectQuery = query.Query.GetObjectQuery(); var futureBatch = QueryFutureManager.AddOrGetBatch(objectQuery.Context); var futureQuery = new QueryFutureValue <TResult>(futureBatch, objectQuery); #elif EF7 var context = query.Query.GetDbContext(); var futureBatch = QueryFutureManager.AddOrGetBatch(context); var futureQuery = new QueryFutureValue <TResult>(futureBatch, query.Query); #endif futureBatch.Queries.Add(futureQuery); return(futureQuery); }
public static void EFPlus_FutureQuery_SingleObjectsAndValues() { CUI.MainHeadline(nameof(EFPlus_FutureQuery_SingleObjectsAndValues)); using (var ctx = new DA.WWWingsContext()) { ctx.Log(); CUI.Headline("Define three future queries ... nothing happens in the database"); QueryFutureValue <int> qPilotCount = ctx.PilotSet.DeferredCount().FutureValue(); QueryFutureValue <DateTime?> qBirthdayOfOldestPilot = ctx.PilotSet.DeferredMin(x => x.Birthday).FutureValue(); QueryFutureValue <Pilot> qOldestPilot = ctx.PilotSet.OrderBy(x => x.Birthday).DeferredFirstOrDefault().FutureValue(); CUI.Headline("We need the values now!"); Console.WriteLine("Number of pilots: " + qPilotCount.Value); Console.WriteLine("Birthday of oldest pilot: " + qBirthdayOfOldestPilot.Value.Value.ToShortDateString()); Console.WriteLine("Name of of oldest pilot: " + qOldestPilot.Value.FullName); } }
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()); }
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 <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()); }