I have a class Pedido
, which has a calculated field, which is the value of the request, consisting of the sum of the items minus the discount.
public class Pedido
{
public ICollection<PedidoItem> Itens { get; set; }
public decimal ValorDesconto { get; set; }
public decimal ValorPedido
{
get { return this.Itens.Sum(e => e.Quantidade * e.Valor) - this.ValorDesconto; }
}
}
public class PedidoItem
{
public int CodigoProduto { get; set; }
public decimal Valor { get; set; }
public decimal Quantidade { get; set; }
}
But if I try to do a query with lambda or linq by filtering the value of the request directly, I'm going to get an error.
// Linq nao reconhece ValorPedido
List<Pedido> pedidos = db.Pedidos.Where(p => p.ValorPedido > x).ToList();
I could do:
List<Pedido> pedidos = db.Pedidos.ToList().Where(p => p.ValorPedido > x).ToList()
But this second approach will bring all requests from the database to filter into memory, which may be bad for performance.
I could do a Linq, grouping, and adding, which would generate a more performative query but I would be repeating the existing logic in the RequestData property of class Pedido
.
Entity 7 is able to interpret ValorPedido
or ValorPedido
would have to have a set
, and my application would feed this attribute (but there are those who say not to store calculated fields)?
A trigger would also work, but I would be stuck with the DBMS, for example, I could not change the provider only. Is there something to be done or is this a limitation of the ORM and period?