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

Using Max( ) is not working after upgrading from 2.2 to 3.1. #20240

Closed
suadev opened this issue Mar 10, 2020 · 4 comments
Closed

Using Max( ) is not working after upgrading from 2.2 to 3.1. #20240

suadev opened this issue Mar 10, 2020 · 4 comments

Comments

@suadev
Copy link

suadev commented Mar 10, 2020

Using Max() is not working after upgrading from 2.2 to 3.1.

The Excption and code :

Processing of the LINQ expression 't => t
    .Where(s => s.Date == t
        .Max(m => m.Date))' by 'NavigationExpandingExpressionVisitor' failed. This may indicate either 
a bug or a limitation in EF Core.
 var groupRates = GetPastRatesAsGroupByCodeId(businessDate);

 return groupRates
                .SelectMany(t =>
                    t.Where(s => s.Date == t.Max(m => m.Date)))  
                .ProjectToType<ExchangeCodeDto>()
                .GetPagedAsync(page, size);

Further technical details

EF Core version:
Database provider: (e.g. Npgsql.EntityFrameworkCore.PostgreSQL)
Target framework: (e.g. .NET Core 3.1.2)
IDE: (e.g. VS Code)

@suadev
Copy link
Author

suadev commented Mar 12, 2020

Update:

I guess this is not about Max(). I changed my query like this and the exception is not changed.

         var groupRates = GetPastRatesAsGroupByCodeId(businessDate);

          return groupRates
                .SelectMany(t => t.Where(s => s.Date == DateTime.Now)) 
                .ProjectToType<ExchangeCodeDto>()
                .GetPagedAsync(page, size);

groupRates comes from the following method. So this issue is about GroupBy or SelectMany ?

      private IQueryable<IGrouping<Guid, ExchangeRate>> GetPastRatesAsGroupByCodeId(DateTime businessDate)
        {
            var query = QueryAsNoTracking().Where(s => s.Date < businessDate); 
            return query.GroupBy(g => g.ExchangeCode.Id);
        }

@suadev
Copy link
Author

suadev commented Mar 12, 2020

Any thoughts or workaround ? @ajcvickers @smitpatel

@smitpatel
Copy link
Member

GroupBy().SelectMany() is not supported since IGrouping does not have SQL representation. Though given SelectMany would flatten out all groupings anyway, probably query can be written differently.
Untested code. Something like below should give you same result.

QueryAsNoTracking().Where(s => s.Date < businessDate).OrderBy(g => g.ExchangeCode.Id).Where(t => t.Date == DateTime.Now).ProjectToType<ExchangeCodeDto>().GetPagedAsync(page, size);

@suadev
Copy link
Author

suadev commented Mar 13, 2020

Ok, as it seems this is duplication of #17068 so i am closing.

@suadev suadev closed this as completed Mar 13, 2020
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants