efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Using OrderBy on property that was resolved using double ternary operator.

Open cts-tradeit opened this issue 1 year ago • 3 comments

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<AccountingJournal>() .OrderByDescending(a => a.WorkerAccountingId != null ? DbSet<Worker>() .Where(w => w.AccountingId == a.WorkerAccountingId) .SingleOrDefault() : a.WorkerEnterpriseId != null ? DbSet<Worker>() .Where(w0 => w0.EnterpriseId == a.WorkerEnterpriseId) .SingleOrDefault() : null.Surname)' could not be translated

cts-tradeit avatar Apr 24 '24 14:04 cts-tradeit

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 avatar Apr 24 '24 14:04 cts-tradeit

Provider being used is npgsql.

cts-tradeit avatar Apr 25 '24 09:04 cts-tradeit

@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.

roji avatar Apr 25 '24 23:04 roji

sorry, i have been busy and forgot about this, will deliver by the end of this week

cts-tradeit avatar Jun 18 '24 16:06 cts-tradeit

Closing as no repro was provided.

cincuranet avatar Jul 19 '24 06:07 cincuranet