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 OrderBy on property that was resolved using double ternary operator. #33607

Open
cts-tradeit opened this issue Apr 24, 2024 · 3 comments
Open

Comments

@cts-tradeit
Copy link

Current behaviour

Sample query from DebugView (the query is way bigger but this is the part that causes the problem):

DbSet<AccountingJournal>()
    .Select(journal => new OverviewAccountingJournalMappingSource{ 
        Worker = journal.WorkerAccountingId != null ? <>c__DisplayClass1_0.workers
            .SingleOrDefault(w => w.AccountingId == journal.WorkerAccountingId) : journal.WorkerEnterpriseId != null ? <>c__DisplayClass1_0.workers
            .SingleOrDefault(w => w.EnterpriseId == journal.WorkerEnterpriseId) : null, 
    )
    .Select(dtoOverviewAccountingJournalMappingSource => new OverviewAccountingJournalWebDto{ 
        WorkerSurname = dtoOverviewAccountingJournalMappingSource.Worker.Surname, 
    }
    )
    .OrderByDescending($it => $it.WorkerSurname)
    .ThenBy($it => $it.Id)
    .Skip(TypedLinqParameterContainer<int>.TypedProperty)
    .Take(TypedLinqParameterContainer<int>.TypedProperty)

Produces:
System.InvalidOperationException: The LINQ expression 'DbSet()
.OrderByDescending(a => a.WorkerAccountingId != null ? DbSet()
.Where(w => w.AccountingId == a.WorkerAccountingId)
.SingleOrDefault() : a.WorkerEnterpriseId != null ? DbSet()
.Where(w0 => w0.EnterpriseId == a.WorkerEnterpriseId)
.SingleOrDefault() : null.Surname)' could not be translated

@cts-tradeit
Copy link
Author

We suspect that it is caused by it being a double ternary operator as this query containing ternary operator works

DbSet<AccountingJournal>()
    .Select(journal => new OverviewAccountingJournalMappingSource{ 
        Client = journal.ClientAccountingId != null ? <>c__DisplayClass1_0.clients
            .SingleOrDefault(c => c.AccountingId == journal.ClientAccountingId) : null, 
    }
    )
    .Select(dtoOverviewAccountingJournalMappingSource => new OverviewAccountingJournalWebDto{ 
        ClientShortcut = dtoOverviewAccountingJournalMappingSource.Client.Shortcut,     
    }
    )
    .OrderBy($it => $it.ClientShortcut)
    .ThenBy($it => $it.Id)
    .Skip(TypedLinqParameterContainer<int>.TypedProperty)
    .Take(TypedLinqParameterContainer<int>.TypedProperty)

@cts-tradeit cts-tradeit changed the title Combining using OrderBy on property that was resolved using ternary operator. Using OrderBy on property that was resolved using double ternary operator. Apr 25, 2024
@cts-tradeit
Copy link
Author

Provider being used is npgsql.

@roji
Copy link
Member

roji commented Apr 25, 2024

@cts-tradeit please take the time to create a minimal, runnable code sample (e.g. a console program) - the above is a very partial snippet without a full exception etc.

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

5 participants