The EF Core WhereBulkContains method from the Entity Framework Extensions library allows you to filter a LINQ query by including all items from an existing list.
For example, imagine you have a list of Customer
objects where the CustomerID
and a few other properties are already populated. You want to retrieve those customers from the database so you can update their values.
The WhereBulkContains
method lets you filter entities based on whatβs in your list:
// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;
var dictDeserializedCustomers = deserializedCustomers.ToDictionary(x => x.CustomerID);
using (var context = new EntityContext())
{
var customers = context.Customers.WhereBulkContains(deserializedCustomers).ToList();
customers.ForEach(x =>
{
var deserializedCustomer = dictDeserializedCustomers[x.CustomerID];
x.Code = deserializedCustomer.Code;
x.Email = deserializedCustomer.Email;
x.FirstName = deserializedCustomer.FirstName;
x.LastName = deserializedCustomer.LastName;
});
// context.SaveChanges();
context.BulkSaveChanges();
}
This method also works with batch operations like DeleteFromQuery and UpdateFromQuery, making it even easier to use in those scenarios:
// @nuget: Z.EntityFramework.Extensions.EFCore
using Z.EntityFramework.Extensions;
var dictDeserializedCustomers = deserializedCustomers.ToDictionary(x => x.CustomerID);
using (var context = new EntityContext())
{
context.Customers.WhereBulkContains(deserializedCustomers).DeleteFromQuery();
context.Customers
.WhereBulkContains(deserializedCustomers)
.UpdateFromQuery(x => new Customer { IsActive = false });
}
This unique method is part of a professional library made by ZZZ Projects. Entity Framework Extensions is the most popular and complete EF Core library available, with over 50 million downloads and more than 5,000 paid customers.
π Why Use the WhereBulkContains
Method from Entity Framework Extensions?
Most developers use the standard Where
+ Contains
combo to filter data in EF Core.
It works fineβ¦ until it doesnβt. Here are some common limitations:
- β Only supports basic types like
int
orGuid
- β Limited by SQL parameter count (you canβt pass too many IDs)
- β Doesnβt support composite keys or other complex scenarios
Thatβs where the WhereBulkContains
method from Entity Framework Extensions really shines β it was built to overcome all of these limitations:
β Supports any kind of data source
- β
Basic types like
List<int>
orList<Guid>
- β
Entity types like
List<Customer>
- β
Anonymous types (e.g.,
new { ID = 1 }
) - β
Even
ExpandoObject
lists
- β
Basic types like
β Handles an unlimited number of items
β Works with surrogate keys and composite keys
No matter how complex your list is, WhereBulkContains
makes filtering just work β and scale.
π¦ How to Install Entity Framework Extensions
To use the WhereBulkContains
method, install the Z.EntityFramework.Extensions.EFCore NuGet package:
PM> NuGet\Install-Package Z.EntityFramework.Extensions.EFCore
> dotnet add package Z.EntityFramework.Extensions.EFCore
Entity Framework Extensions uses EF Core pinned versioning, so make sure the first digit of the package version matches your EF Core version. For example:
- If you're using
EF Core 9
, use versionv9.x.y.z
- If you're using
EF Core 8
, use versionv8.x.y.z
- And so on...
Looking for a different version, like WhereBulkContains
for EF6?
π Download it here
β Final Thoughts
When it comes to filtering large datasets in EF Core, the WhereBulkContains
method from Entity Framework Extensions is a powerful tool you shouldnβt overlook.
It works with any data source, supports unlimited items, and handles complex scenarios that the standard Where
+ Contains
method just canβt. Whether you're working with basic ID lists or full entities, this method helps you write cleaner, faster, and more efficient queries.
Give it a try and see how much smoother your data filtering can be!
π External Links
Looking for more resources? Here are some helpful links to learn more about the WhereBulkContains
method and Entity Framework Extensions, explore its documentation, and download the right version for your project.