dotnet / dotnet/efcore

Translate non-constant inline arrays/lists to JSON array creator

Open
#36,679 2 comments 0 reactions 0 assignees View on GitHub
area-bulkupdate area-json area-query
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

There are scenarios where an inline array or list (`new[] { ... }`) is used; when such inline collections only contain constants/parameters, they are replaced in the funcletizer by a simple constant/parameter and everything works well. However, if one of the inline elements references a database value, that can't happen, and the query fails as we don't translate NewArrayExpression and ListInitExpression.

Examples include:

```c#
// Direct list comparison for scalar collections:
_ = await context.Blogs.Where(b => b.SomeInts == new[] { 3, b.Foo })).ToListAsync();

// Rearrange structural collection property (see test AssociationsBulkUpdateTestBase.Update_collection_referencing_the_original_collection)
_ = context.Blogs.ExecuteUpdateAsync(s => s.SetProperty(b => b.SomeJson.SomeObjects, b => new[] { b.SomeJson.SomeObjects[1], b.SomeJson.SomeObjects[0] });
```

Database typically provide ways to construct a JSON array (e.g. [JSON_ARRAY()](https://learn.microsoft.com/en-us/sql/t-sql/functions/json-array-transact-sql?view=sql-server-ver17) in SQL Server). The tricky aspect is to know that we're in JSON context when translating the NewArrayExpression. That's a bit similar to our existing type inference machinery, but the collection can also be of structural types (where we have no type mappings and so no inference).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.