Skip to content

Commit c9c15c3

Browse files
committed
Document EF.Functions.JsonPathExists() (#5272)
See dotnet/efcore#31136
1 parent b52f5e2 commit c9c15c3

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

entity-framework/core/providers/sql-server/functions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ stringValue.TrimStart() | LTRIM(
236236

237237
.NET | SQL | Added in
238238
----------------------------------------------------------------- | --------------------------------------------------------- | --------
239-
EF.Functions.JsonContains(json, searchValue, path?, searchMode?) | JSON_CONTAINS(@json, @searchValue, @path?, @searchMode?) | EF Core 11.0
239+
EF.Functions.JsonContains(json, searchValue, path?, searchMode?) | JSON_CONTAINS(@json, @searchValue, @path?, @searchMode?) | EF 11.0
240+
EF.Functions.JsonPathExists(json, path) | JSON_PATH_EXISTS(@json, @path) | EF 11.0
240241

241242
## Miscellaneous functions
242243

entity-framework/core/what-is-new/ef-core-11.0/whatsnew.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ ORDER BY (
104104

105105
Similarly, `MinByAsync` orders ascending and returns the element with the minimum value for the key selector.
106106

107+
### EF.Functions.JsonPathExists()
108+
109+
EF Core 11 introduces `EF.Functions.JsonPathExists()`, which checks whether a given JSON path exists in a JSON document. On SQL Server, this translates to the [`JSON_PATH_EXISTS`](/sql/t-sql/functions/json-path-exists-transact-sql) function (available since SQL Server 2022).
110+
111+
The following query filters blogs to those whose JSON data contains an `OptionalInt` property:
112+
113+
```csharp
114+
var blogs = await context.Blogs
115+
.Where(b => EF.Functions.JsonPathExists(b.JsonData, "$.OptionalInt"))
116+
.ToListAsync();
117+
```
118+
119+
This generates the following SQL:
120+
121+
```sql
122+
SELECT [b].[Id], [b].[Name], [b].[JsonData]
123+
FROM [Blogs] AS [b]
124+
WHERE JSON_PATH_EXISTS([b].[JsonData], N'$.OptionalInt') = 1
125+
```
126+
127+
`EF.Functions.JsonPathExists()` accepts a JSON value and a JSON path to check for. It can be used with scalar string properties, complex types, and owned entity types mapped to JSON columns.
128+
129+
For the full `JSON_PATH_EXISTS` SQL Server documentation, see [`JSON_PATH_EXISTS`](/sql/t-sql/functions/json-path-exists-transact-sql).
130+
107131
## Cosmos DB
108132

109133
<a name="cosmos-transactional-batches"></a>

0 commit comments

Comments
 (0)