CSharp · Console
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Data.SqlClient;
using System.Linq;
using System.Collections.Generic;
public class Order
{
public int Id { get; set; }
public string Description { get; set; }
public bool IsProcessed { get; set; }
public string CustomerName { get; set; }
public decimal Amount { get; set; }
public int Quantity { get; set; }
public string Status { get; set; }
public DateTime CreatedDate { get; set; }
public string Address { get; set; }
public string Notes { get; set; }
}
public class AppDbContext : DbContext
{
public DbSet<Order> Orders { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
}
}
public class Program
{
public static async Task Main()
{
using var context = new AppDbContext();
// Ensure database is created
await context.Database.EnsureCreatedAsync();
if (!context.Orders.Any())
{
var orders = Enumerable.Range(1, 10000).Select(i => new Order
{
Description = $"Order {i}",
CustomerName = $"Customer {i}",
Quantity = i * 2,
Amount = 10.5m * i,
CreatedDate = DateTime.UtcNow.AddDays(-i),
IsProcessed = i % 2 == 0,
Address = $"Address {i}, {i} + 1",
Notes = $"Notes {i}"
}).ToList();
var stopwatch = Stopwatch.StartNew();
await context.BulkInsertAsync(orders);
stopwatch.Stop();
Console.WriteLine($"Inserted {orders.Count} orders");
Console.WriteLine($"Elapsed: {Math.Round(stopwatch.Elapsed.TotalSeconds, 2)} seconds");
var processedOrders = orders.Where(o => o.IsProcessed).ToList();
stopwatch.Restart();
var processedOrdersRecords = await context.Orders.BulkReadAsync(processedOrders);
stopwatch.Stop();
Console.WriteLine($"There are {processedOrdersRecords.Count} Processed orders");
Console.WriteLine($"There are {processedOrdersRecords} Processed orders");
Console.WriteLine($"Elapsed: {Math.Round(stopwatch.Elapsed.TotalSeconds, 2)} seconds");
List<string> customerNames = ["Customer 1", "Customer 3", "Customer 7", "Customer 9", "Customer 23"];
var ordersWithFivePrimeCustomerNames = context.Orders
.WhereBulkContains(customerNames, o => o.CustomerName)
.AsNoTracking()
.ToList();
var lowQuantities = orders.Where(o => o.Quantity <= 10000);
var ordersWithHighQuantities = await context.Orders.WhereBulkNotContains(lowQuantities).ToListAsync();
Console.WriteLine($"There are {ordersWithHighQuantities.Count} Orders with High Quantities");
var first100Batch = Enumerable.Range(1, 100).Select(i => new Order
{
Description = $"Order {i}",
CustomerName = $"Customer {i}",
Quantity = i * 2,
Amount = 10.5m * i,
CreatedDate = DateTime.UtcNow.AddDays(-i),
IsProcessed = i % 2 == 0,
Address = $"Address {i}, {i} + 1",
Notes = $"Notes {i}"
}).ToList();
var first100BatchOfOrders = context.Orders.WhereBulkContainsFilterList(first100Batch, x => x.Quantity);
Console.WriteLine($"first100BatchOfOrders {first100BatchOfOrders.Count} ");
var last1000Batch = Enumerable.Range(9001, 10000).Select(i => new Order
{
Description = $"Order {i}",
CustomerName = $"Customer {i}",
Quantity = i * 2,
Amount = 10.5m * i,
CreatedDate = DateTime.UtcNow.AddDays(-i),
IsProcessed = i % 2 == 0,
Address = $"Address {i}, {i} + 1",
Notes = $"Notes {i}"
}).ToList();
var last1000BatchOfOrders = context.Orders.WhereBulkNotContainsFilterList(last1000Batch, x => new {x.CustomerName, x.Quantity});
Console.WriteLine($"last1000BatchOfOrders {last1000BatchOfOrders.Count} ");
}
}
}