Allows you to efficiently compose an IEnumerable<T> in your Entity Framework Core queries when using the SQL Server Database Provider. This is accomplished by using the AsQueryableValues extension method available on the DbContext class. Everything is evaluated on the server with a single round trip, in a way that preserves the query's execution plan, even when the values behind the IEnumerable<T> are changed on subsequent executions.
$ dotnet add package BlazarTech.QueryableValues.SqlServer🤔💭 TLDR; By using QueryableValues, you can incorporate in-memory collections into your EF queries with outstanding performance and flexibility.
This library allows you to efficiently compose an IEnumerable<T> in your Entity Framework Core queries when using the SQL Server Database Provider. You can accomplish this by using the AsQueryableValues extension method that's available on the DbContext class. The query is processed in a single round trip to the server, in a way that preserves its execution plan, even when the values within the IEnumerable<T> are changed on subsequent executions.
Highlights
For a detailed explanation of the problem solved by QueryableValues, please continue reading .
💡 Still on Entity Framework 6 (non-core)? Then QueryableValues
EF6 Editionis what you need.
If you feel that this solution has provided you some value, please consider buying me a ☕.
Your ⭐ on this repository also helps! Thanks! 🖖🙂
QueryableValues is distributed as a NuGet Package. The major version number of this library is aligned with the version of Entity Framework Core by which it's supported (e.g. If you are using EF Core 5, then you must use version 5 of QueryableValues).
Look for the place in your code where you are setting up your DbContext and calling the UseSqlServer extension method, then use a lambda expression to access the SqlServerDbContextOptionsBuilder provided by it. It is on this builder that you must call the UseQueryableValues extension method as shown in the following simplified examples:
When using the OnConfiguring method inside your DbContext:
using BlazarTech.QueryableValues;
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
"MyConnectionString",
sqlServerOptionsBuilder =>
{
sqlServerOptionsBuilder.UseQueryableValues();
}
);
}
}When setting up the DbContext at registration time using dependency injection:
using BlazarTech.QueryableValues;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<MyDbContext>(optionsBuilder => {
optionsBuilder.UseSqlServer(
"MyConnectionString",
sqlServerOptionsBuilder =>
{
sqlServerOptionsBuilder.UseQueryableValues();
}
);
});
}
}💡
UseQueryableValuesoffers an optionaloptionsdelegate for additional configurations.
The AsQueryableValues extension method is provided by the BlazarTech.QueryableValues namespace; therefore, you must add the following using directive to your source code file for it to appear as a method of your DbContext instance:
using BlazarTech.QueryableValues;💡 If you access your DbContext via an interface, you can also make the
AsQueryableValuesextension methods available on it by inheriting from theIQueryableValuesEnabledDbContextinterface.
Below are a few examples composing a query using the values provided by an IEnumerable<T>.
💡 Supports Byte, Int16, Int32, Int64, Decimal, Single, Double, DateTime, DateTimeOffset, DateOnly, TimeOnly, Guid, Char, String, and Enum.
Using the Contains LINQ method:
// Sample values.
IEnumerable<int> values = Enumerable.Range(1, 10);
// Example #1 (LINQ method syntax)
var myQuery1 = dbContext.MyEntities
.Where(i => dbContext
.AsQueryableValues(values)
.Contains(i.MyEntityID)
)
.Select(i => new
{
i.MyEntityID,
i.PropA
});
// Example #2 (LINQ query syntax)
var myQuery2 =
from i in dbContext.MyEntities
where dbContext
.AsQueryableValues(values)
.Contains(i.MyEntityID)
select new
{
i.MyEntityID,
i.PropA
};Using the Join LINQ method:
// Sample values.
IEnumerable<int> values = Enumerable.Range(1, 10);
// Example #1 (LINQ method syntax)
var myQuery1 = dbContext.MyEntities
.Join(
dbContext.AsQueryableValues(values),
i => i.MyEntityID,
v => v,
(i, v) => new
{
i.MyEntityID,
i.PropA
}
);
// Example #2 (LINQ query syntax)
var myQuery2 =
from i in dbContext.MyEntities
join v in dbContext.AsQueryableValues(values) on i.MyEntityID equals v
select new
{
i.MyEntityID,
i.PropA
};💡 Must be an anonymous or user-defined type with one or more simple type properties, including Boolean.
// Performance Tip:
// If your IEnumerable<T> item type (T) has many properties, project only
// the ones you need to a new variable and use it in your query.
var projectedItems = items.Select(i => new { i.CategoryId, i.ColorName });
// Example #1 (LINQ method syntax)
var myQuery1 = dbContext.Product
.Join(
dbContext.AsQueryableValues(projectedItems),
p => new { p.CategoryId, p.ColorName },
pi => new { pi.CategoryId, pi.ColorName },
(p, pi) => new
{
p.ProductId,
p.Description
}
);
// Example #2 (LINQ query syntax)
var myQuery2 =
from p in dbContext.Product
join pi in dbContext.AsQueryableValues(projectedItems) on new { p.CategoryId, p.ColorName } equals new { pi.CategoryId, pi.ColorName }
select new
{
p.ProductId,
p.Description
};About Complex Types
⚠️ All the data provided by this type is transmitted to the server; therefore, ensure that it only contains the properties you need for your query. Not following this recommendation will degrade the query's performance.
⚠️ There is a limit of up to 10 properties for any given simple type (e.g. cannot have more than 10 Int32 properties). Exceeding that limit will cause an exception and may also suggest that you should rethink your strategy.
Please take a look at the repository.