8
I have a class Pedido
, which has a calculated field, which is the order value, composed 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 order value directly, I will 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 database requests to filter in 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 logic already existing in the Value property of the class Pedido
.
Entity 7 is able to interpret ValorPedido
or the ValorPedido
would have to have a set
, and my application feed this attribute(but there are those who say that it should not store calculated fields)?
A Rigger would also solve, but I would be stuck with the SGBD, 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?
By coincidence I was seeing about it today. And from what I understand, you don’t have much to do.
– Jéf Bueno
See http://stackoverflow.com/questions/15585330/calculated-column-in-ef-code-first.
– Marconi