Skip to content

Use ExecuteUpdateAsync to remove array item #3523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
s-horbach opened this issue Apr 18, 2025 · 1 comment
Closed

Use ExecuteUpdateAsync to remove array item #3523

s-horbach opened this issue Apr 18, 2025 · 1 comment

Comments

@s-horbach
Copy link

s-horbach commented Apr 18, 2025

Hi all 👋

9.0 Release introduced one more great feature of translating array.Where(i => i != x) to array_remove(array, x) and i tried to use that, but unfortunately without any success.

So i'd really appreciate any help 🙏

My case:

Table schema:

UserId: uuid Items: text[]
ce8b8d26-1575-4f32-8226-e71f75bbc1f0 {Item1,Item2}

c#:

await dbContext
    .UserItems
    .Where(x => x.UserId == userId) // filter by UserId
    .Where(x => x.Items.Where(item => item == itemToDelete).Any()) // make sure it has an item we'd like to delete
    .ExecuteUpdateAsync(setter => setter.SetProperty(
        prop => prop.Items,
        value => value.Items.Where(item => item != itemToDelete)));

Actual result: The LINQ expression 'item => item != __itemToDelete_1' could not be translated.

Expected result: i was expecting EF to generate something like this:

UPDATE "userItems" AS u
SET "items" = array_remove(u."items", @__item_1)
WHERE u."userId" = @__userId_0 AND EXISTS (
  SELECT 1
  FROM unnest(u."items") AS i(value)
  WHERE i.value = @__item_1)

PS as a fallback i can always use ExecuteSqlAsync, but i just wanted to have it beautiful, via LINQ 🤩

PSPS replacing value.Items.Where(item => item != itemToDelete... with Append( - works flawlessly and generates SQL with array_append function

Thanks in advance for any help or hint.

@roji
Copy link
Member

roji commented Apr 20, 2025

There are unfortunately some EF-side limitations on using collections inside ExecuteUpdate - you'll have to make do with raw SQL for now.

@roji roji closed this as completed Apr 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants