Skip to content
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

Bug: COALESCE uses only first and second argument #2596

Closed
boukeversteegh opened this issue Dec 11, 2022 · 2 comments
Closed

Bug: COALESCE uses only first and second argument #2596

boukeversteegh opened this issue Dec 11, 2022 · 2 comments

Comments

@boukeversteegh
Copy link

Summary:

It seems impossible to generate COALESCE(a, b, c, d). Only COALESCE(a, b) will be generated.

Details:

I have a custom localization function that is defined as such:

modelBuilder
            .HasDbFunction(
                typeof(MyDbContext)
                    .GetMethod(nameof(Localize),
                        new[] { typeof(Dictionary<string, string>), typeof(string[]) })!
            )
            .HasTranslation(args => {
               var languages = ((SqlConstantExpression)args[1]).Value as string[];
               var arguments = MakeLanguageFieldsExpressions(languages);
               return new SqlFunctionExpression(
                    "COALESCE",
                    arguments,
                    false,
                    new bool[arguments.Length],
                    typeof(string),
                    null
                );
           });

Now in a LINQ query, I can call Items.select(item => new LocalizedItem { title = Localize(item, new [] {"en", "de"} ) } , and the query will be translated to (details omitted):

SELECT COALESCE(e."Title"#>>'{en,value}', e."Title"#>>'{de,value}') AS "Title"

This works great, however this only works with 2 coalescing arguments. Anything after the 2nd argument is never generated into SQL.

How can I work around this limitation?

@boukeversteegh
Copy link
Author

Apparently this is fixed in EF Core 7.

Solved using EF Core 7 with Dotnet 6.

@boukeversteegh
Copy link
Author

boukeversteegh commented Dec 11, 2022

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Dec 11, 2022
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