Is there a faster (performative) way of comparing the current value with the previous value (of the bank) with Entity Framework ? Instead of selecting Id
by Id
(as Discount
property of the code below), check the array
integer, or something of the type?
With the code below, I check the last price of the value and calculate the discount, however, it is done Id
by Id
.
ha.AddRange(
array.Select(
x => new ProductHistory {
ProductId = x.Id
, Price = x.Price
, LastUpdate = DateTime.Now.Date
, Discount = x.Price / (
(Convert.ToDecimal(db.ProductHistory.Where((ProductHistory p) => x.Id == p.ProductId)
.OrderByDescending(o => o.LastUpdate)
.Select(y => y.Price)
.FirstOrDefault()) == 0) ? x.Price :
(Convert.ToDecimal(db.ProductHistory.Where((ProductHistory p) => x.Id == p.ProductId)
.OrderByDescending(o => o.Id)
.Select(y => y.Price)
.FirstOrDefault()))) * 100 - 100
}
)
);