CSharp · Console
// Entity Framework Extensions
// Doc: https://entityframework-extensions.net/where-bulk-contains
// @nuget: Microsoft.EntityFrameworkCore.SqlServer
// @nuget: Z.EntityFramework.Extensions.EFCore
using System.Collections.Generic;
using System.Linq;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
public class Program
{
public static void Main()
{
// Doc: https://entityframework-extensions.net/where-bulk-contains
using (var context = new EntityContext())
{
context.Database.EnsureCreated();
// SEED
var customer1 = new Customer() { Name = "Jonathan" };
var customer2 = new Customer() { Name = "ZZZ Projects" };
var customer3 = new Customer() { Name = "Sara" };
context.Orders.Add(new Order() { TotalQuantity = 13, Customer = customer1 });
context.Orders.Add(new Order() { TotalQuantity = 66, Customer = customer2 });
context.Orders.Add(new Order() { TotalQuantity = 99, Customer = customer3 });
context.Orders.Add(new Order() { TotalQuantity = 101, Customer = customer1 });
context.SaveChanges();
}
using (var context = new EntityContext())
{
List<string> customerNames = new List<string>() { "Jonathan", "ZZZ Projects"};
var orders = context.Orders.Include(x => x.Customer).WhereBulkContains(x => x.Customer, customerNames, x => x.Name).ToList();
FiddleHelper.WriteTable(orders);
}
}
public class EntityContext : DbContext
{
static EntityContext()
{
using (var context = new EntityContext())
{
context.Database.EnsureCreated();
}
}
public DbSet<Order> Orders { get; set; }
public DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
}
}
public class Order
{
public int OrderID { get; set; }
public int TotalQuantity { get; set; }
public Customer Customer { get; set; }
}
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
}
}