Click here to Skip to main content
15,446,510 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, is this possible to write SQL-translatable query to order by a dictionary table?

For instance:
class Customer
{
   string Name {get;set;}
   IEnumerable<CustomProperty> CustomProps {get;set;}
}

class CustomProperty
{
   string Name {get;set;}
   string Value {get;set;}
}

And for instance I'd like to order by CustomProperty named Color.

What I have tried:

_dbContext.Customers
.Include(x=>CustomProps)
.OrderBy(x => x.CustomProps.Where(x => x.Name == "Color")
                           .SelectMany(x => x.Value)


Ends up with non-translatable sql, obviously.
The LINQ expression 'x => x.Value' could not be translated.


Is this doable in EF Core 5 or the ordering has to be done on client side (API)?
Posted
Updated 31-Aug-21 0:41am

1 solution

Try:
C#
.OrderBy(x => x.CustomProps.Where(p => p.Name == "Color").Select(p => p.Value).FirstOrDefault())
Or:
C#
.OrderBy(x => x.CustomProps.Where(p => p.Name == "Color").Select(p => p.Value).Min())
 
Share this answer
 
v2
Comments
Maciej Los 31-Aug-21 13:13pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900