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 when using DatabaseContext in Computed method #152

Closed
Aleksaas opened this issue Sep 18, 2019 · 4 comments
Closed

Bug when using DatabaseContext in Computed method #152

Aleksaas opened this issue Sep 18, 2019 · 4 comments

Comments

@Aleksaas
Copy link

I have a method like this:

        [Computed]
        public bool ForWeek(DateTime? dateFrom, DatabaseContext ctx)
        {
            return DateFrom <= dateFrom
                && ctx.ShiftPeriods.Max(s => s.DateFrom) == DateFrom;
        }

Ignore the logic, its only for testing :) But when it is compiled query looks like this:

from ShiftPeriod s in DbSet<ShiftPeriod>
select s.DateFrom).Max() == [e].DateFrom

And this is query without delegate decompiler:

from ShiftPeriod s in DbSet<ShiftPeriod>
where (Nullable<DateTime>)[s].DateFrom <= __dateFrom_3
select (Nullable<DateTime>)[s].DateFrom).Max() == (Nullable<DateTime>)[e].DateFrom

See how 's' is without '[]' in DD query, which leads to optimized query looks like:

from ShiftPeriod d in DbSet<ShiftPeriod>
where !([d].Deleted)
select s.DateFrom).Max() == [d].DateFrom

And I get error:
variable 's' of type 'ShiftPeriod' referenced from scope '', but it is not defined

So I assume there is bug when DatabseContext is used inside a computed method :) Or maybe DD is not supposed to work like that and I should not use DatabseContext inside computed method?

@magicmoux
Copy link
Contributor

Could you please provide a full sample to check this further?
Thx in advance
Max

@hazzik hazzik changed the title Bug when using DatbaseContext in Computed method Bug when using DatabaseContext in Computed method Feb 3, 2021
cervengoc pushed a commit to cervengoc/DelegateDecompiler that referenced this issue May 27, 2021
@cervengoc
Copy link
Contributor

Hi @hazzik, @magicmoux ,

We've just ran into the same issue, and I've added a failing test for it. Could you please have a look at my branch?

Br, Zoltán

@magicmoux
Copy link
Contributor

Hi @cervengoc ,

As I suspected the problem is that DD currently handles System.Linq.Expressions.Expression.xxx methods verbatim, which finally confuses the ELinq translator.

At the time I proposed #151 to handle these calls.
I just upgraded it to include the latest changes from hazzik/develop (as a hot-fix) and updated your tests for POC.

magicmoux added a commit to magicmoux/DelegateDecompiler that referenced this issue Sep 22, 2022
Possible fix hazzik#122
Feature prop to enable closure for local IQueryable
@magicmoux magicmoux mentioned this issue Sep 22, 2022
@hazzik
Copy link
Owner

hazzik commented Oct 13, 2022

This should be fixed by #192

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

4 participants