You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This library 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.
6
+
This library 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.
@@ -22,7 +22,7 @@ It provides a solution to the following long standing [EF Core issue](https://gi
22
22
## Getting Started
23
23
24
24
### Installation
25
-
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).
25
+
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).
26
26
27
27
Please choose the appropriate command below to install it using the NuGet Package Manager Console window in Visual Studio:
28
28
@@ -144,8 +144,8 @@ var myQuery2 =
144
144
#### Complex Type Example
145
145
```c#
146
146
// Performance Tip:
147
-
// If your IEnumerable<T> variable’s item type has many properties,
148
-
//project only the ones you need to a new variable and use it in your query.
147
+
// If your IEnumerable<T> item type (T) has many properties, project only
148
+
// the ones you need to a new variable and use it in your query.
> :warning: 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.
161
+
> :warning: 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.
162
162
163
163
> :warning: 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.
164
164
@@ -180,9 +180,9 @@ var myQuery = dbContext.MyEntities
180
180
i.PropC
181
181
});
182
182
```
183
-
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 underline 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.
183
+
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 underline 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.
184
184
185
-
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:
185
+
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:
186
186
187
187
```c#
188
188
varlistOfValues=newList<int> { 1, 2, 3 };
@@ -207,20 +207,20 @@ WHERE [m].[MyEntityID] IN (1, 2, 3) OR ([m].[PropB] = @__p_1)',N'@__p_1 bigint',
207
207
```
208
208
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.
209
209
210
-
Now, let’s add another item to the list of values and execute the query again:
210
+
Now, let's add another item to the list of values and execute the query again:
WHERE [m].[MyEntityID] IN (1, 2, 3, 4) OR ([m].[PropB] = @__p_1)',N'@__p_1 bigint',@__p_1=100
215
215
```
216
-
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.
216
+
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.
217
217
218
218
## Enter AsQueryableValues 🙌
219
219

220
220
221
221
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.
222
222
223
-
Let’s take a look at the following query making use of this method, which is functionally equivalent to the previous example:
223
+
Let's take a look at the following query making use of this method, which is functionally equivalent to the previous example:
224
224
```c#
225
225
varmyQuery=dbContext.MyEntities
226
226
.Where(i=>
@@ -246,7 +246,7 @@ WHERE EXISTS (
246
246
) AS [q]
247
247
WHERE [q].[V] = [m].[MyEntityID]) OR ([m].[PropB] = @__p_1)',N'@p0 xml,@__p_1 bigint',@p0=@p3,@__p_1=100
248
248
```
249
-
Now, let’s add another item to the list of values and execute the query again:
249
+
Now, let's add another item to the list of values and execute the query again:
250
250
```tsql
251
251
declare @p3 xml
252
252
set @p3=convert(xml,N'<R><V>1</V><V>2</V><V>3</V><V>4</V></R>')
@@ -262,19 +262,19 @@ WHERE EXISTS (
262
262
Great! The SQL statement provided to [sp_executesql] remains constant. In this case SQL Server can reuse the [execution plan] from the previous execution.
263
263
264
264
## The Numbers 📊
265
-
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][ContainsEnumerable] LINQ method (orange), and then five executions of the second one making use of the `AsQueryableValues` extension method on the [DbContext] (green):
265
+
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][ContainsEnumerable] LINQ method (orange), and then five executions of the second one making use of the `AsQueryableValues` extension method on the [DbContext] (green):
266
266
267
267

268
268
<sup>Queries executed against SQL Server 2017 Express (14.0.2037) running on a resource constrained laptop.</sup>
269
269
270
270
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.
271
271
272
-
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.
272
+
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.
273
273
274
274
## What Makes This Work? 🤓
275
-
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].
275
+
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].
276
276
277
-
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.
277
+
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.
278
278
279
279
## One More Thing 👀
280
280
The `AsQueryableValues` extension method allows you to treat a sequence of values as you normally would if these were another entity in your [DbContext]. The type returned by the extension is an [IQueryable\<T\>] that can be composed with other entities in your query.
Copy file name to clipboardExpand all lines: docs/README.md
+28-22Lines changed: 28 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,36 +1,36 @@
1
1
# QueryableValues
2
2
3
-
This library allows us to efficiently compose an [IEnumerable<T>] in our[Entity Framework Core] queries when using the [SQL Server Database Provider]. This is done by using the `AsQueryableValues` extension method that is made available on the [DbContext] class. Everything is evaluated on the server with a single roundtrip, in a way that preserves the query's [execution plan], even when the values behind the [IEnumerable<T>] are changed on subsequent executions.
3
+
This library 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.
- Can be a userdefined class or struct, with read/write properties and a public constructor.
10
-
- Must have one or more simple type properties.
9
+
- Can be a user-defined class or struct with read/write properties and a public constructor.
10
+
- Must have one or more simple type properties, including [Boolean].
11
11
12
12
For a detailed explanation, please continue reading [here][readme-background].
13
13
14
-
## When Should I Use It?
15
-
The `AsQueryableValues` extension method is intended for queries that are dependent on a *non-constant* sequence of external values. In this case, the underline SQL query will be efficient on subsequent executions.
14
+
## When Should You Use It?
15
+
The `AsQueryableValues` extension method is intended for queries that are dependent upon a *non-constant* sequence of external values. In such cases, the underlying SQL query will be efficient on subsequent executions.
16
16
17
-
It provides a solution to the following long standing EF Core [issue](https://github.com/dotnet/efcore/issues/13617) and enables other currently unsupported scenarios; like the ability to efficiently create joins with in-memory data.
17
+
It provides a solution to the following long standing [EF Core issue](https://github.com/dotnet/efcore/issues/13617) and enables other currently unsupported scenarios; like the ability to efficiently create joins with in-memory data.
18
18
19
19
## Getting Started
20
20
21
21
### Installation
22
-
QueryableValues is distributed as a [NuGet Package]. The major version number of this library is aligned with the version of [Entity Framework Core]that's supported by it; for example, if you are using EF Core 5, then you must use version 5 of QueryableValues.
22
+
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).
23
23
24
24
Please choose the appropriate command below to install it using the NuGet Package Manager Console window in Visual Studio:
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:
33
+
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:
34
34
35
35
When using the `OnConfiguring` method inside your [DbContext]:
36
36
```c#
@@ -71,13 +71,13 @@ public class Startup
71
71
}
72
72
```
73
73
74
-
### How Do I Use It?
75
-
The `AsQueryableValues` extension method is provided by the `BlazarTech.QueryableValues` namespace, therefore, you must add the following `using` directive to your source code file in order for it to appear as a method of your [DbContext] instance:
74
+
### How Do You Use It?
75
+
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:
76
76
```
77
77
using BlazarTech.QueryableValues;
78
78
```
79
79
80
-
Below you can find a few examples composing a query using the values provided by an [IEnumerable<T>].
80
+
Below are a few examples composing a query using the values provided by an [IEnumerable<T>].
81
81
82
82
#### Simple Type Examples
83
83
Using the [Contains][ContainsQueryable] LINQ method:
@@ -138,10 +138,11 @@ var myQuery2 =
138
138
i.PropA
139
139
});
140
140
```
141
-
#### Complex Type Examples
141
+
#### Complex Type Example
142
142
```c#
143
-
// If your IEnumerable<T> variable's item type is a complex type with many properties,
144
-
// project only what you need to a new variable and use it in your query.
143
+
// Performance Tip:
144
+
// If your IEnumerable<T> item type (T) has many properties, project only
145
+
// the ones you need to a new variable and use it in your query.
> :warning: All the data provided by this type is transmitted to the server, therefore, ensure that it only contains the properties that you need for your query. Not following this recommendation will degrade the query's performance.
158
+
> :warning: 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.
158
159
159
-
> :warning: There is a limit of up to ten properties for any given simple type (e.g., cannot have more than ten[Int32] properties). Exceeding that limit will cause an exception and may also be a sign that you should rethink your strategy.
160
+
> :warning: 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.
160
161
161
-
## Do You Want to Know More? 📚
162
-
Please take a look at the repository [here](https://github.com/yv989c/BlazarTech.QueryableValues).
162
+
## Do You Want To Know More? 📚
163
+
Please take a look at the [repository](https://github.com/yv989c/BlazarTech.QueryableValues).
0 commit comments