Git Product home page Git Product logo

blazartech.queryablevalues's Introduction

Logo

QueryableValues

MIT License GitHub Stars Nuget Downloads

🤔💭 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

  • ✨ Enables the composition of in-memory data within your queries, utilizing both simple and complex types.
  • 👌 Works with all versions of SQL Server supported by Entity Framework Core.
  • ⚡ Automatically uses the most efficient strategy compatible with your SQL Server instance and configuration.
  • ✅ Boasts over 140 tests for reliability and compatibility, giving you added confidence.

For a detailed explanation of the problem solved by QueryableValues, please continue reading here.

💡 Still on Entity Framework 6 (non-core)? Then QueryableValues EF6 Edition is what you need.

Your Support is Appreciated!

If you feel that this solution has provided you some value, please consider buying me a ☕.

Buy me a coffee

Your ⭐ on this repository also helps! Thanks! 🖖🙂

Getting Started

Installation

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).

Configuration

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();
                }
            );
        });
    }
}

💡 UseQueryableValues offers an optional options delegate for additional configurations.

How Do You Use It?

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 AsQueryableValues extension methods available on it by inheriting from the IQueryableValuesEnabledDbContext interface.

Below are a few examples composing a query using the values provided by an IEnumerable<T>.

Simple Type Examples

💡 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
    };

Complex Type Example

💡 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.

Benchmarks

The following benchmarks consist of simple EF Core queries that have a dependency on a random sequence of Int32, Guid, and String values via the Contains LINQ method. It shows the performance differences between not using and using QueryableValues. In practice, the benefits of using QueryableValues are more dramatic on complex EF Core queries and busy environments.

Benchmarked Libraries

Package Version
Microsoft.EntityFrameworkCore.SqlServer 8.0.0
BlazarTech.QueryableValues.SqlServer 8.1.0

BenchmarkDotNet System Specs and Configuration

BenchmarkDotNet v0.13.10, Windows 11 (10.0.22621.2715/22H2/2022Update/SunValley2)
AMD Ryzen 9 6900HS Creator Edition, 1 CPU, 16 logical and 8 physical cores
.NET SDK 8.0.100
  [Host]     : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
  Job-EBAAJF : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2

Server=True  InvocationCount=200  IterationCount=25
RunStrategy=Monitoring  UnrollFactor=1  WarmupCount=1

SQL Server Instance Specs

Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64) 
Oct  8 2022 05:58:25 
Copyright (C) 2022 Microsoft Corporation
Express Edition (64-bit) on Windows 10 Pro 10.0 <X64> (Build 22621: ) (Hypervisor)
  • The SQL Server instance was running in the same system where the benchmarks were executed.
  • Shared Memory is the only network protocol that's enabled on this instance.

Query Duration - Without vs. With (XML) vs. With (JSON)

Legend:

  • Without: Plain EF.
  • With (XML): EF with QueryableValues using the XML serializer.
  • With (JSON): EF with QueryableValues using the JSON serializer.

Benchmarks Chart

Method Type NumberOfValues Mean Error StdDev Median Ratio RatioSD Gen0 Gen1 Gen2 Allocated Alloc Ratio
Without Int32 2 1,167.3 us 43.27 us 57.77 us 1,143.9 us 1.00 0.00 - - - 8.7 KB 1.00
WithXml Int32 2 526.3 us 14.62 us 19.51 us 520.8 us 0.45 0.03 - - - 44.86 KB 5.16
WithJson Int32 2 432.1 us 16.57 us 22.12 us 427.6 us 0.37 0.02 - - - 31.3 KB 3.60
Without Int32 8 1,953.4 us 54.65 us 72.95 us 1,959.4 us 1.00 0.00 - - - 8.77 KB 1.00
WithXml Int32 8 591.3 us 29.30 us 39.11 us 595.6 us 0.30 0.02 - - - 45.42 KB 5.18
WithJson Int32 8 440.7 us 11.51 us 15.37 us 439.7 us 0.23 0.01 - - - 31.77 KB 3.62
Without Int32 32 2,662.0 us 103.32 us 137.93 us 2,688.7 us 1.00 0.00 - - - 9.12 KB 1.00
WithXml Int32 32 822.3 us 70.84 us 94.57 us 876.3 us 0.31 0.04 - - - 48.2 KB 5.29
WithJson Int32 32 481.1 us 21.07 us 28.13 us 475.6 us 0.18 0.01 - - - 33.88 KB 3.72
Without Int32 128 2,490.2 us 103.31 us 137.91 us 2,490.8 us 1.00 0.00 - - - 15.37 KB 1.00
WithXml Int32 128 1,941.4 us 96.33 us 128.59 us 1,877.3 us 0.78 0.05 - - - 59.58 KB 3.88
WithJson Int32 128 604.4 us 47.16 us 62.96 us 624.6 us 0.24 0.03 - - - 41.5 KB 2.70
Without Int32 512 2,546.6 us 129.47 us 172.84 us 2,567.5 us 1.00 0.00 - - - 22.7 KB 1.00
WithXml Int32 512 6,416.5 us 59.68 us 79.67 us 6,403.5 us 2.53 0.17 - - - 112.25 KB 4.95
WithJson Int32 512 989.7 us 138.60 us 185.03 us 900.3 us 0.39 0.07 - - - 73.62 KB 3.24
Without Int32 2048 2,632.8 us 130.88 us 174.72 us 2,636.6 us 1.00 0.00 - - - 65.01 KB 1.00
WithXml Int32 2048 24,377.3 us 236.77 us 316.09 us 24,251.8 us 9.30 0.64 - - - 346.68 KB 5.33
WithJson Int32 2048 2,601.5 us 225.17 us 300.60 us 2,479.2 us 0.99 0.13 - - - 204.41 KB 3.14
Without Guid 2 2,058.0 us 32.38 us 43.22 us 2,047.2 us 1.00 0.00 - - - 8.92 KB 1.00
WithXml Guid 2 517.1 us 18.83 us 25.14 us 515.7 us 0.25 0.01 - - - 45.23 KB 5.07
WithJson Guid 2 440.0 us 16.36 us 21.84 us 438.1 us 0.21 0.01 - - - 31.56 KB 3.54
Without Guid 8 5,811.5 us 30.72 us 41.01 us 5,806.7 us 1.00 0.00 - - - 14.17 KB 1.00
WithXml Guid 8 573.1 us 32.34 us 43.17 us 580.0 us 0.10 0.01 - - - 46.64 KB 3.29
WithJson Guid 8 461.8 us 17.77 us 23.72 us 460.0 us 0.08 0.00 - - - 32.66 KB 2.30
Without Guid 32 20,328.2 us 63.94 us 85.36 us 20,340.1 us 1.00 0.00 - - - 17.83 KB 1.00
WithXml Guid 32 771.5 us 77.67 us 103.68 us 838.1 us 0.04 0.01 - - - 52.9 KB 2.97
WithJson Guid 32 525.1 us 12.43 us 16.60 us 521.5 us 0.03 0.00 - - - 36.57 KB 2.05
Without Guid 128 80,563.7 us 695.46 us 928.42 us 80,338.0 us 1.000 0.00 - - - 45.43 KB 1.00
WithXml Guid 128 1,631.3 us 103.83 us 138.60 us 1,556.0 us 0.020 0.00 - - - 77.96 KB 1.72
WithJson Guid 128 740.5 us 71.79 us 95.83 us 786.8 us 0.009 0.00 - - - 52.08 KB 1.15
Without Guid 512 330,720.6 us 646.57 us 863.15 us 330,831.7 us 1.000 0.00 - - - 133.83 KB 1.00
WithXml Guid 512 5,109.5 us 146.38 us 195.41 us 5,056.7 us 0.015 0.00 - - - 184.93 KB 1.38
WithJson Guid 512 1,547.3 us 145.16 us 193.78 us 1,425.7 us 0.005 0.00 - - - 115.97 KB 0.87
Without Guid 2048 1,434,232.2 us 4,863.00 us 6,491.96 us 1,431,593.8 us 1.000 0.00 5.0000 5.0000 5.0000 562.98 KB 1.00
WithXml Guid 2048 19,451.1 us 75.68 us 101.03 us 19,443.0 us 0.014 0.00 - - - 637.14 KB 1.13
WithJson Guid 2048 5,226.9 us 214.80 us 286.75 us 5,140.3 us 0.004 0.00 - - - 372.87 KB 0.66
Without String 2 976.7 us 40.55 us 54.14 us 971.1 us 1.00 0.00 - - - 9.65 KB 1.00
WithXml String 2 540.1 us 13.88 us 18.53 us 538.6 us 0.55 0.03 - - - 45.82 KB 4.75
WithJson String 2 516.0 us 21.13 us 28.21 us 514.0 us 0.53 0.04 - - - 32.14 KB 3.33
Without String 8 2,481.9 us 43.38 us 57.91 us 2,479.8 us 1.00 0.00 - - - 14.96 KB 1.00
WithXml String 8 608.7 us 30.48 us 40.69 us 614.0 us 0.25 0.02 - - - 47.42 KB 3.17
WithJson String 8 617.3 us 18.08 us 24.14 us 613.0 us 0.25 0.01 - - - 33.66 KB 2.25
Without String 32 8,006.9 us 44.74 us 59.73 us 8,010.0 us 1.00 0.00 - - - 20.39 KB 1.00
WithXml String 32 838.6 us 83.21 us 111.08 us 906.2 us 0.10 0.01 - - - 53.9 KB 2.64
WithJson String 32 849.1 us 64.67 us 86.33 us 892.0 us 0.11 0.01 - - - 37.95 KB 1.86
Without String 128 31,372.5 us 58.88 us 78.60 us 31,382.7 us 1.00 0.00 - - - 52.91 KB 1.00
WithXml String 128 1,802.2 us 146.18 us 195.14 us 1,690.7 us 0.06 0.01 - - - 79.74 KB 1.51
WithJson String 128 1,863.0 us 130.62 us 174.38 us 1,758.8 us 0.06 0.01 - - - 56.59 KB 1.07
Without String 512 133,130.3 us 634.15 us 846.57 us 133,481.7 us 1.00 0.00 - - - 165.83 KB 1.00
WithXml String 512 5,911.6 us 134.15 us 179.08 us 5,872.8 us 0.04 0.00 - - - 190.35 KB 1.15
WithJson String 512 6,672.9 us 165.38 us 220.78 us 6,638.3 us 0.05 0.00 - - - 131.24 KB 0.79
Without String 2048 535,679.0 us 977.69 us 1,305.19 us 535,368.9 us 1.00 0.00 5.0000 5.0000 5.0000 687.4 KB 1.00
WithXml String 2048 22,191.9 us 65.79 us 87.83 us 22,189.3 us 0.04 0.00 - - - 655.65 KB 0.95
WithJson String 2048 27,953.3 us 133.58 us 178.32 us 27,962.5 us 0.05 0.00 - - - 432.27 KB 0.63

Version Archive


Background 📚

When Entity Framework Core is set up to use the SQL Server Database Provider and it detects the use of variables in a query, in most cases it provides its values as parameters to an internal SqlCommand object that will execute the translated SQL statement. This is done efficiently by using the sp_executesql stored procedure behind the scenes, so if the same SQL statement is executed a second time, the SQL Server instance will likely have a computed execution plan in its cache, thereby saving time and system resources.

The Problem 🤔

We have been in the situation where we need to build a query that must return one or more items based on a sequence of values. The common pattern to do this makes use of the Contains LINQ extension method on the IEnumerable<T> interface, then we pass the property of the entity that must match any of the values in the sequence. This way we can retrieve multiple items with a single round trip to the database as shown in the following example:

var myQuery = dbContext.MyEntities
    .Where(i => listOfValues.Contains(i.MyEntityID))
    .Select(i => new
    {
        i.MyEntityID,
        i.PropB,
        i.PropC
    });

The previous query will yield the expected results, but there's a catch. If the sequence of values in our list is different on every execution, the underlying SQL query will be built in a way that's not optimal for SQL Server's query engine. Wasting system resources like CPU, memory, IO, and potentially affecting other queries in the instance.

Let's take a look at the following query and the SQL that is generated by the SQL Server Database Provider as of version 5.0.11 when the query is materialized:

var listOfValues = new List<int> { 1, 2, 3 };
var anotherVariable = 100;
var myQuery = dbContext.MyEntities
    .Where(i =>
        listOfValues.Contains(i.MyEntityID) ||
        i.PropB == anotherVariable
    )
    .Select(i => new
    {
        i.MyEntityID,
        i.PropA
    })
    .ToList();

Generated SQL

exec sp_executesql N'SELECT [m].[MyEntityID], [m].[PropA]
FROM [dbo].[MyEntity] AS [m]
WHERE [m].[MyEntityID] IN (1, 2, 3) OR ([m].[PropB] = @__p_1)',N'@__p_1 bigint',@__p_1=100

Here we can observe that the values in our list are being hardcoded as part of the SQL statement provided to sp_executesql as opposed to them being injected via a parameter, as is the case for our other variable holding the value 100.

Now, let's add another item to the list of values and execute the query again:

exec sp_executesql N'SELECT [m].[MyEntityID], [m].[PropA]
FROM [dbo].[MyEntity] AS [m]
WHERE [m].[MyEntityID] IN (1, 2, 3, 4) OR ([m].[PropB] = @__p_1)',N'@__p_1 bigint',@__p_1=100

As we can see, a new SQL statement was generated just because we modified the list that's being used in our Where predicate. This has the detrimental effect that a previously cached execution plan cannot be reused, forcing SQL Server's query engine to compute a new execution plan every time it is provided with a SQL statement that it hasn't seen before and increasing the likelihood of flushing other plans in the process.

💡 To address this issue, EF8 now incorporates the use of the OPENJSON function when possible. The change was tracked by this EF Core issue. As of EF version 8.0.0, QueryableValues remains superior in terms of compatibility and performance.

Enter AsQueryableValues 🙌

Parameterize All the Things

This library provides you with the AsQueryableValues extension method made available on the DbContext class. It solves the problem explained above by allowing you to build a query that will generate a SQL statement for sp_executesql that will remain constant execution after execution, allowing SQL Server to do its best every time by using a previously cached execution plan. This will speed up your query on subsequent executions, and conserve system resources.

Let's take a look at the following query making use of this method, which is functionally equivalent to the previous example:

var myQuery = dbContext.MyEntities
    .Where(i =>
        dbContext.AsQueryableValues(listOfValues).Contains(i.MyEntityID) ||
        i.PropB == anotherVariable
    )
    .Select(i => new
    {
        i.MyEntityID,
        i.PropA
    });

Generated SQL

declare @p3 xml
set @p3=convert(xml,N'<R><V>1</V><V>2</V><V>3</V></R>')
exec sp_executesql N'SELECT [m].[MyEntityID], [m].[PropA]
FROM [dbo].[MyEntity] AS [m]
WHERE EXISTS (
    SELECT 1
    FROM (
        SELECT I.value(''. cast as xs:integer?'', ''int'') AS [V] FROM @p0.nodes(''/R/V'') N(I)
    ) AS [q]
    WHERE [q].[V] = [m].[MyEntityID]) OR ([m].[PropB] = @__p_1)',N'@p0 xml,@__p_1 bigint',@p0=@p3,@__p_1=100

Now, let's add another item to the list of values and execute the query again:

declare @p3 xml
set @p3=convert(xml,N'<R><V>1</V><V>2</V><V>3</V><V>4</V></R>')
exec sp_executesql N'SELECT [m].[MyEntityID], [m].[PropA]
FROM [dbo].[MyEntity] AS [m]
WHERE EXISTS (
    SELECT 1
    FROM (
        SELECT I.value(''. cast as xs:integer?'', ''int'') AS [V] FROM @p0.nodes(''/R/V'') N(I)
    ) AS [q]
    WHERE [q].[V] = [m].[MyEntityID]) OR ([m].[PropB] = @__p_1)',N'@p0 xml,@__p_1 bigint',@p0=@p3,@__p_1=100

Great! The SQL statement provided to sp_executesql remains constant. In this case SQL Server can reuse the execution plan from the previous execution.

The Numbers 📊

You don't have to take my word for it! Let's see a trace of what's going on under the hood when both of these queries are executed multiple times, adding a new value to the list after each execution. First, five executions of the one making direct use of the Contains LINQ method (orange), and then five executions of the second one making use of the AsQueryableValues extension method on the DbContext (green):

Trace Queries executed against SQL Server 2017 Express (14.0.2037) running on a resource constrained laptop.

As expected, none of the queries in the orange section hit the cache. On the other hand, after the first query in the green section, all the subsequent ones hit the cache and consumed fewer resources.

Now, focus your attention to the first query of the green section. Here you can observe that there's a cost associated with this technique, but this cost can be offset in the long run, especially when your queries are not trivial like the ones in these examples.

What Makes This Work? 🤓

🎉 QueryableValues now supports JSON serialization, which improves its performance compared to using XML. By default, QueryableValues will attempt to use JSON if it is supported by your SQL Server instance and database configuration.

QueryableValues makes use of the XML parsing capabilities in SQL Server, which are available in all the supported versions of SQL Server to date. The provided sequence of values are serialized as XML and embedded in the underlying SQL query using a native XML parameter, then it uses SQL Server's XML type methods to project the query in a way that can be mapped by Entity Framework Core.

This is a technique that I have not seen being used by other popular libraries that aim to solve this problem. It is superior from a latency standpoint because it resolves the query with a single round trip to the database and most importantly, it preserves the query's execution plan even when the content of the XML is changed.

Did You Find a 🐛 or Have an 💡?

PRs are welcome! 🙂

blazartech.queryablevalues's People

Contributors

yv989c avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

blazartech.queryablevalues's Issues

The generic AsQueryableValues throws if options are provided..

Hi,

Thanks for the great work on this library. I just tested the latest release with Json serialization and performance improvements are great!

I'm working on some extensions so I can support the in-memory provider (details here), and I noticed a small issue. I'm unable to use properly the generic overload.

This works

using var dbContext = new AppDbContextQueryableJSON();
var names = new string[] { "abc", "def" };
var query = dbContext.Customers.Where(x => dbContext.AsQueryableValues(names, true).Contains(x.Name));
var result = await query.ToListAsync();

This fails

using var dbContext = new AppDbContextQueryableJSON();
var names = new string[] { "abc", "def" };
var query = dbContext.Customers.Where(x => dbContext.AsQueryableValues(names, opt => opt.DefaultForIsUnicode(true)).Contains(x.Name));
var result = await query.ToListAsync();

Stack trace:

System.InvalidOperationException
  HResult=0x80131509
  Message=The LINQ expression '__8__locals2_dbContext_0
    .AsQueryableValues(
        values: __names_1, 
        configure: opt => opt.DefaultForIsUnicode(True))
    .Contains(NavigationTreeExpression
        Value: EntityReference: Customer
        Expression: c.Name)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ExpandNavigationsForSource(NavigationExpansionExpression source, Expression expression)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessLambdaExpression(NavigationExpansionExpression source, LambdaExpression lambdaExpression)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.ProcessWhere(NavigationExpansionExpression source, LambdaExpression predicate)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
   at Microsoft.EntityFrameworkCore.Query.Internal.NavigationExpandingExpressionVisitor.Expand(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
   at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__65`1.MoveNext()

Any feedback is appreciated.

ArgumentException when writing strings with control characters

The XML 1.0 spec which is used by the underline XmlWriter does not allow some control characters and may be others.

I don't think that the current runtime exception provides a good UX.

Some options:

  • Translate these invalid XML characters to a common placeholder.
  • Remove these invalid XML characters from the string.

I believe this is a corner case though but is still a limitation because these characters can be stored using the varchar and nvarchar data types in SQL Server.

Relevant info: https://stackoverflow.com/a/28152666/2206145

Json Serialization Support

Background

As of now, QueryableValues relies on the XML capabilities of SQL Server in order to efficiently project the in-memory values in a T-SQL query. These capabilities are available in all supported versions of SQL Server to date.

JSON support was introduced in SQL Server 2016. Early benchmarks shows that JSON performs close to 1 order of magnitude faster than its XML counterpart, therefore, I'm considering providing support for it. This can be achieved via OPENJSON.

Caveats

OPENJSON is only available under the following conditions:

  • SQL Server 2016 or higher.
  • Database compatibility mode 130 or higher.

Configuration API Proposal

...
sqlServerOptionsBuilder.
    UseQueryableValues(options => options.UseJson(Auto|Always|Never));
...

Enum Support

I've a service that builds a dynamic query WHERE condition based on some filters.
I'm trying to use your library to build one of those filter, related to an array of enum values, but I'm getting this Exception at runtime:

The type MyProject.Api.Data.Models.OperationType must have at least one public property.

The user can select one or more of those values and so the WHERE condition has to be built using OR.

The MyProject.Api.Data.Models.OperationType is an enum type defined as follows:

public enum OperationType : byte
{
    DT = 1,
    NP = 2,
    Tx = 3,
    RE = 4,
    I = 5,
    AT = 6,
    M = 7,
    TR = 8,
    TxAT = 9
}

My actual code for building the query is this:

...
if (filter.OperationTypes?.Any() == true)
{
    // Conversion from API enum model to EF enum model
    var operationTypes = filter.OperationTypes.Select(c => Enum.Parse<Data.Models.OperationType>(c.ToString(), true)).ToArray();
	
    // Call to AsQueryableValues that causes the Exception
    var queryableValues = _dbContext.AsQueryableValues(operationTypes);

    query = query.Where(x => x.Movements.OperationType != null && queryableValues.Contains(x.Movements.OperationType.Value));
}
...

Am I doing something wrong? How am I supposed to use the library with enums?

QueryableValues fails when joining against a query that uses the Include method

As reported in #30 (comment)

When materializing the query, it fails with a System.InvalidOperationException exception:

Unable to translate a collection subquery in a projection since either parent or the subquery doesn't project necessary information required to uniquely identify it and correctly generate results on the client side. This can happen when trying to correlate on keyless entity type. This can also happen for some cases of projection before 'Distinct' or some shapes of grouping key in case of 'GroupBy'. These should either contain all key properties of the entity that the operation is applied on, or only contain simple property access expressions.

Should this library be creating tables in a migration?

I just noticed in my last EF Core migration it picked up these table properties not sure why but didn't see any documentation if this is required

I have multiple contexts that are registering UseQueryableValues

Of the 9 contexts, when I generate migrations - one of them (ConfigurationContext) - is creating tables that the others are not

Startup.cs

            builder.Services.AddDbContext<AccumulationContext>(options =>
                options.UseSqlServer(appSettings.ConnectionStrings.Accumulation,
                    sqlServerOptionsBuilder => { sqlServerOptionsBuilder.UseQueryableValues(); }));

            builder.Services.AddDbContext<ConfigurationContext>(options =>
                options.UseSqlServer(appSettings.ConnectionStrings.Configuration,
                    sqlServerOptionsBuilder => { sqlServerOptionsBuilder.UseQueryableValues(); }));

            builder.Services.AddDbContext<DispenseCaptureContext>(options =>
                options.UseSqlServer(appSettings.ConnectionStrings.DispenseCapture,
                    sqlServerOptionsBuilder => { sqlServerOptionsBuilder.UseQueryableValues(); }));

            etc.

ConfigurationContextModelSnapshot.cs

        protected override void BuildModel(ModelBuilder modelBuilder)
        {
#pragma warning disable 612, 618
            modelBuilder
                .HasAnnotation("ProductVersion", "6.0.4")
                .HasAnnotation("Relational:MaxIdentifierLength", 128);

            SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);

            modelBuilder.Entity("BlazarTech.QueryableValues.QueryableValuesEntity", b =>
                {
                    b.Property<bool?>("Bool0")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool1")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool2")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool3")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool4")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool5")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool6")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool7")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool8")
                        .HasColumnType("bit");

                    b.Property<bool?>("Bool9")
                        .HasColumnType("bit");

                    b.Property<byte?>("Byte0")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte1")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte2")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte3")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte4")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte5")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte6")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte7")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte8")
                        .HasColumnType("tinyint");

                    b.Property<byte?>("Byte9")
                        .HasColumnType("tinyint");

                    b.Property<string>("Char0")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char1")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char2")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char3")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char4")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char5")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char6")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char7")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char8")
                        .HasColumnType("nvarchar(1)");

                    b.Property<string>("Char9")
                        .HasColumnType("nvarchar(1)");

                    b.Property<DateTime?>("DateTime0")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime1")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime2")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime3")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime4")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime5")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime6")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime7")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime8")
                        .HasColumnType("datetime2");

                    b.Property<DateTime?>("DateTime9")
                        .HasColumnType("datetime2");

                    b.Property<DateTimeOffset?>("DateTimeOffset0")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset1")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset2")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset3")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset4")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset5")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset6")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset7")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset8")
                        .HasColumnType("datetimeoffset");

                    b.Property<DateTimeOffset?>("DateTimeOffset9")
                        .HasColumnType("datetimeoffset");

                    b.Property<decimal?>("Decimal0")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal1")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal2")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal3")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal4")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal5")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal6")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal7")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal8")
                        .HasColumnType("decimal(18,6)");

                    b.Property<decimal?>("Decimal9")
                        .HasColumnType("decimal(18,6)");

                    b.Property<double?>("Double0")
                        .HasColumnType("float");

                    b.Property<double?>("Double1")
                        .HasColumnType("float");

                    b.Property<double?>("Double2")
                        .HasColumnType("float");

                    b.Property<double?>("Double3")
                        .HasColumnType("float");

                    b.Property<double?>("Double4")
                        .HasColumnType("float");

                    b.Property<double?>("Double5")
                        .HasColumnType("float");

                    b.Property<double?>("Double6")
                        .HasColumnType("float");

                    b.Property<double?>("Double7")
                        .HasColumnType("float");

                    b.Property<double?>("Double8")
                        .HasColumnType("float");

                    b.Property<double?>("Double9")
                        .HasColumnType("float");

                    b.Property<float?>("Float0")
                        .HasColumnType("real");

                    b.Property<float?>("Float1")
                        .HasColumnType("real");

                    b.Property<float?>("Float2")
                        .HasColumnType("real");

                    b.Property<float?>("Float3")
                        .HasColumnType("real");

                    b.Property<float?>("Float4")
                        .HasColumnType("real");

                    b.Property<float?>("Float5")
                        .HasColumnType("real");

                    b.Property<float?>("Float6")
                        .HasColumnType("real");

                    b.Property<float?>("Float7")
                        .HasColumnType("real");

                    b.Property<float?>("Float8")
                        .HasColumnType("real");

                    b.Property<float?>("Float9")
                        .HasColumnType("real");

                    b.Property<Guid?>("Guid0")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid1")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid2")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid3")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid4")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid5")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid6")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid7")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid8")
                        .HasColumnType("uniqueidentifier");

                    b.Property<Guid?>("Guid9")
                        .HasColumnType("uniqueidentifier");

                    b.Property<int?>("Int0")
                        .HasColumnType("int");

                    b.Property<int?>("Int1")
                        .HasColumnType("int");

                    b.Property<int?>("Int2")
                        .HasColumnType("int");

                    b.Property<int?>("Int3")
                        .HasColumnType("int");

                    b.Property<int?>("Int4")
                        .HasColumnType("int");

                    b.Property<int?>("Int5")
                        .HasColumnType("int");

                    b.Property<int?>("Int6")
                        .HasColumnType("int");

                    b.Property<int?>("Int7")
                        .HasColumnType("int");

                    b.Property<int?>("Int8")
                        .HasColumnType("int");

                    b.Property<int?>("Int9")
                        .HasColumnType("int");

                    b.Property<long?>("Long0")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long1")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long2")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long3")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long4")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long5")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long6")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long7")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long8")
                        .HasColumnType("bigint");

                    b.Property<long?>("Long9")
                        .HasColumnType("bigint");

                    b.Property<short?>("Short0")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short1")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short2")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short3")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short4")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short5")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short6")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short7")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short8")
                        .HasColumnType("smallint");

                    b.Property<short?>("Short9")
                        .HasColumnType("smallint");

                    b.Property<string>("String0")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String1")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String2")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String3")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String4")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String5")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String6")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String7")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String8")
                        .HasColumnType("nvarchar(max)");

                    b.Property<string>("String9")
                        .HasColumnType("nvarchar(max)");

                    b.ToTable("QueryableValuesEntity");

                    b.ToView("QueryableValuesEntity");
                });

image

Non-intrusive Mode

Description

Automatically treats IEnumerable<T> types composed in a LINQ expression as if they were provided via the AsQueryableValues method. I'm assuming that the direct use of the IEnumerable<T> type is likely to have a non-constant sequence of values.

There's also the legitime case of not wanting to use QueryableValues. Like having a small list of constant values that I want hardcoded in the T-SQL, so I can use T[] or List<T> for these.

Some desired attributes:

  • Must be opt-in via configuration.
  • Must allow the registration of other types that also implement IEnumerable<T> so they can also be treated this way per user needs.

Examples

With non-intrusive mode On, the following two queries will use QueryableValues (in both cases the values will be parameterized in the T-SQL query):

IEnumerable<int> values = Enumerable.Range(1, 10);

var myQuery1 = 
    from i in dbContext.MyEntities
    where dbContext
        .AsQueryableValues(values)
        .Contains(i.MyEntityID)
    select new
    {
        i.MyEntityID,
        i.PropA
    };

var myQuery2 = 
    from i in dbContext.MyEntities
    where values.Contains(i.MyEntityID)
    select new
    {
        i.MyEntityID,
        i.PropA
    };

With non-intrusive mode On, the first query will use QueryableValues and the second will not (the values will be hardcoded in the T-SQL instead of being parameterized):

List<int> values = Enumerable.Range(1, 10).ToList();

var myQuery1 = 
    from i in dbContext.MyEntities
    where dbContext
        .AsQueryableValues(values)
        .Contains(i.MyEntityID)
    select new
    {
        i.MyEntityID,
        i.PropA
    };

var myQuery2 = 
    from i in dbContext.MyEntities
    where values.Contains(i.MyEntityID)
    select new
    {
        i.MyEntityID,
        i.PropA
    };

Motivation

Not having to introduce an alien method (AsQueryableValues) in our EF queries is better for portability.

Ideas

Maybe be something around rewriting the original LINQ expression at some point in the EF query processing pipeline. Find IEnumerable<T> and replace with AsQueryableValues(DbContext, IEnumerable<T>).

EFCore InternalServiceProvider is not supported

Background

QueryableValues are not working when using EFCore InternalServiceProvider due to missing service registration in the DI container.

Unable to resolve service for type 'BlazarTech.QueryableValues.IQueryableFactory'.
This is often because no database provider has been configured for this DbContext.
A provider can be configured by overriding the 'DbContext.OnConfiguring' method
or by using 'AddDbContext' on the application service provider.
If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.

EF Core registration

DI registration of EF is given this way:

services.AddDbContext<TestDbContext>((provider, builder) =>
        {
            builder.UseSqlServer(connectionString, opt =>
            {
                opt.UseQueryableValues();
            });
            builder.UseInternalServiceProvider(EntityFrameworkServices.Build(provider));
        });

public static class EntityFrameworkServices
{
    private static IServiceProvider _efServices;

    public static IServiceProvider Build(IServiceProvider serviceProvider)
    {
        if (_efServices != null)
            return _efServices;

        var efServices = new ServiceCollection()
            .AddEntityFrameworkSqlServer();

        return _efServices = efServices.BuildServiceProvider();
    }
}

Test environment

.NET 6 runtime

<PackageReference Include="BlazarTech.QueryableValues.SqlServer" Version="6.5.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.5" />

Initial Investigation

Looks like the method QueryableValuesSqlServerExtension.ApplyServices is called on the instance of IServiceCollection that is not really taking part in the creation of the ServiceProvider since the instance of the ServiceProvider has already been built manually.

ToQueryString does not work

I got an issue, while trying to read a query plain text

Min example:

using BlazarTech.QueryableValues;
using Microsoft.EntityFrameworkCore;

var dbContext = new ApplicationContext();
var codes = new[] { 1, 2 };

var query = dbContext.Docs
    .Where(x => dbContext.AsQueryableValues(codes).Contains(x.Id));

Console.WriteLine(query.ToQueryString());
public class doc
{
    public int Id { get; set; }
}
public class ApplicationContext : DbContext
{
    public DbSet<doc> Docs { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(
            "..........",
            o => o.UseQueryableValues());
    }
}

The example above throws an exception:

System.InvalidCastException: Unable to cast object of type 'BlazarTech.QueryableValues.DeferredInt32Values' to type 'System.String'.

Enviroment:
BlazarTech.QueryableValues 1.0.9
.net 7.0.202
ef core 7.0.4

Is there a way to compose a query that uses a condition of contains in two lists (AND not OR)?

Hey @yv989c question for you,

We have a service that is querying to find packages that contain items from list 1 and list 2,

This ends up pulling back packages that satisfy list 1 or list 2,

Is there a way to write this so we get a list of packages back that contain both using the queryable values? (They can be large lists)

Thanks!

    public async Task<List<Package>> GetPackagesAsync(List<int> list1, List<string> list2)
    {
        return await _context.Package
            .Where(p => _context
                .AsQueryableValues(list1)
                .Contains(p.p1))
            .Where(p => _context
                .AsQueryableValues(list2, true)
                .Contains(p.p2))
            .ToListAsync();
    }

Question: Nullable columns

Hi,
I'm trying to convert a few of my existing queries to use your library and one of them is causing an issue.

I've got a Guid? column in my table that I'm attempting to do a Contains on.

Using .Value will throw an error because it causes client side evaluation.

I get the below error:

Error	CS1929	'IQueryable<Guid>' does not contain a definition for 'Contains' and the best extension method overload 'MemoryExtensions.Contains<Guid?>(ReadOnlySpan<Guid?>, Guid?)' requires a receiver of type 'System.ReadOnlySpan<System.Guid?>'

Is there any alternative/recommended approach?

Thanks in advance.

EDIT:

I think I've resolved it by using the Join function instead.
Thanks for the useful tool.

Missing types

Add support for:

  • System.Byte
  • System.Int16
  • System.Single
  • System.Boolean (on complex type only)
  • System.Char

Possibility of using implicit version of OpenJson?

Currently the SQL generated by this library invokes OpenJson with explicit mappings, for example:

declare @p0 nvarchar(max) = '[{"X":0,"I":123},...,{"X:"99","I":987}]' // 100 integer values
declare @__p_1 int = 3000
declare @__p_2 int = 100

SELECT [r].[Id], [r].[Amount], [r.AccountId]
FROM [Transactions] AS [r]
WHERE EXISTS (
    SELECT 1
    FROM (
        SELECT TOP(@p1) [X], [I]
        FROM OPENJSON(@p0) WITH ([X] int, [I] int)
        ORDER BY [X]
    ) AS [b]
    WHERE [b].[I] = [r].[AccountId]) // AccountId is an integer
OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY

From my testing the above is slower than using the implicit mappings version:

declare @p0 nvarchar(max) = '[123,...,987]' // 100 integer values
declare @__p_1 int = 3000
declare @__p_2 int = 100

SELECT [r].[Id], [r].[Amount], [r.AccountId]
FROM [Transactions] AS [r]
WHERE EXISTS (
    SELECT 1
    FROM OPENJSON(@p0) AS [b]
    WHERE CONVERT(INT, [b].[value]) = [r].[AccountId])
OFFSET @__p_1 ROWS FETCH NEXT @__p_2 ROWS ONLY

because the former query incurs Sort and Top operations, versus only a Compute Scalar for the former. The latter also sends a smaller amount of data to the server in the JSON-serialised parameter.

What is the reason for using the explicit mappings version instead of implicit? And would you be willing to add functionality to control whether implicit or explicit is used?

Better execution plans

It has caught my attention that the use of the OPENJSON function is causing performance regressions in EF8, as reported in dotnet/efcore#32394. This seems to be caused by the opaque nature of the function to the query engine, which could result in suboptimal execution plans.

QueryableValues currently depends on both OPENJSON and XML methods for functionality, and thus, it is impacted to some degree by these issues.

Here are some potential solutions to address this problem:

  • For large collections, project the deserialized values to a temp table and use the temp table instead. This should enable statistics, which in turn should generate optimal execution plans. However, it may cause execution plan re-compilations and result in higher latency.
  • For small collections, avoid the use of deserialization functions and instead switch to a multi-parameter approach. This approach should generate optimal execution plans and maintain low latency.

These two approaches aim to provide the query engine with sufficient information to generate optimal execution plans, which is a priority. Consequently, this behavior should be enabled by default, with an option for users to opt out if desired.

MySql support?

Hey,

just wanted to ask you if MySql is also supported, because I couldn´t find any information about it.

Doesn't work with compiled models

If we use this with compiled models, the special types used behind the scenes breaks the build.

After rebuilding/regenerating our compiled models we got a generated model that references the internal types SimpleQueryableValuesEntity, ComplexQueryableValuesEntity and QueryableValuesEntity (and possibly some more that I didn't catch).

CompiledModels/ComplexQueryableValuesEntityEntityType.cs(3432,35): error CS0122: 'ComplexQueryableValuesEntity' is inaccessible due to its protection level

Got almost 400 variations of that error in different generated files. And since it breaks the build, I need to manually clean this up since rebuilding these files requires the project to build in the first place.

The simplest solution would be to just make these classes public, maybe put into a Internal namespace or something to signal that they shouldn't be used directly by the end user.

Does/Can this work with enum values?

I notice there's no overloads specifically for them, so i tried the generic overload

public static IQueryable AsQueryableValues(this DbContext dbContext, IEnumerable values, Action<EntityOptionsBuilder>? configure = null) where TSource : notnull

but that resulted in an exception.

Is this a supported scenario already?

Incompatibility with Microsoft.IO.RecyclableMemoryStream 3.0.0

On .NET 8, using latest non-preview version (8.1.0) says it depends on
Microsoft.IO.RecyclableMemoryStream >= 2.3.2
which allows 3.0.0. However, when run in Json mode, you end up with

System.MissingMethodException: Method not found: 'System.IO.MemoryStream Microsoft.IO.RecyclableMemoryStreamManager.GetStream()'.
   at BlazarTech.QueryableValues.Serializers.JsonSerializer.<Serialize>g__GetJson|2_2[T](IEnumerable`1 values, Action`2 writeValue, Func`2 mustSkipValue)
   at BlazarTech.QueryableValues.Serializers.JsonSerializer.Serialize[T](IEnumerable`1 values, IReadOnlyList`1 propertyMappings)
   at BlazarTech.QueryableValues.DeferredValues`3.ToString(IFormatProvider provider)

with a csproj with

<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />

in it. Unfortunately I have another dependency that requires Microsoft.IO.RecyclableMemoryStream >= 3.0.0 so I can't revert to 2.3.x, so I'll have to drop AsQueryableValues for now. Figured I'd let you know so you can consume latest or constrain that dependency at least!

Unable to cast object of type 'BlazarTech.QueryableValues.DeferredInt32Values' to type 'System.String'.

Hi,

Started using this library, thank you for writing it!

When we have non-performant queries our production logging services try to record the query using EFCore's .ToQueryString() method. Once we add QueryableValues to the query however, any attempt to view the generated SQL fail with this exception. This fails for .ToQueryString() and within the debugger's DebugView.Query property as well.

take care
jasen

EF.Property<T> throws exception with list used in AsQueryableValues() extension

EF.Property throws exception with list used in AsQueryableValues() extension,

Details:
EF Core Version: 8.0.1
Database Engine: SQL Server
Exception: "The EF.Property method may only be used within Entity Framework LINQ queries"
Note: Reverse engineering "scaffolding" was used to automatically generate the dbcontext

There're some cases where we cannot read properties directly and we need to use EF.Property instead, this extension does not work with AsQueryableValues list and throws the exception, I understand this exception is normally raised when client evaluation happens instead of server evaluation, so maybe when we use EF.Property along with AsQueryableValues it forces client evaluation for some reason, example with temporal tables below:

var modificationLogs = dbContext.Set<ObjectName>().TemporalAll().AsQueryable();
var properties = new List<string>() { "Name", "Code", "ValidFrom", "ValidTo" };
var query = from modificationLog1 in modificationLogs
            join modificationLog2 in modificationLogs on EF.Property<DateTime>(modificationLog1, "ValidTo") equals EF.Property<DateTime>(modificationLog2, "ValidFrom")
            from property in dbContext.AsQueryableValues(properties, true)
            select new ()
            {
                Date = EF.Property<DateTime>(modificationLog1, "ValidTo").ToString(),                     // this one works correctly,
                Field = property,                        // this one works correctly,
                From = EF.Property<object>(modificationLog1, "Name").ToString()                     // this one works correctly,
                // From = EF.Property<object>(modificationLog1, property).ToString()                     // this one "when enabled" does not work and throws the exception
            };

var items = await query.ToListAsync();                            // exception thrown here

Edit #1: Fixed Typo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.