How can EF Core project computed properties to SQL instead of calculating them on the client side?
public enum OrderType
{
Normal,
Special,
}
public class Order
{
public int SpecialContainerId? { get; set; }
public OrderType CalculatedOrderType { get => SpecialContainerId == null ? OrderType.Normal: OrderType.Special; }
}
// Version 1
dbContext.Order.Where(o => o.CalculatedOrderType == OrderType.Special);
// Version 2
dbContext.Order.Where(o => o.SpecialContainerId != null);
There are two versions for my EF Core query.
The first one is slow, the second fast, because the first one is computed at the client side, and the other in the database.
Is it possible to write computed properties on the Order
class which are projected to SQL in an easy way? I do not want to repeat my query all the time.
Writing a method on order which returns expression would work of course. But is there an easy way available meanwhile?