I need to write a query to get all the shipping costs of sales, and compare it against an estimated shipping cost. Here is my query:
var sales = (from sale in db.Sales
where sale.DateOfSale > startDate && sale.DateOfSale < endDate
group sale by new {sale.ItemID, sale.EstimatedShipping} into g
select new
{
ItemID = g.Key.ItemID
Estimate = g.Key.EstimatedShipping
ActualShipCosts = (from gSales in g select gSales.ActualShipping)
}).ToList();
It seems that doing anything with the group that isn't getting the group by keys or doing g.Count() makes the query run terribly slow, I can't get this query to finish without timing out. Is there anything I can do to help performance here?
You can try performing the ActualShipping selection while you are building the result set:
var sales = db.Sales
.Where(sale => sale.DateOfSale > startDate && sale.DateOfSale < endDate)
.GroupBy(
sale => new {sale.ItemID, sale.EstimatedShipping},
sale => sale.ActualShipping)
.ToList();
Not sure, but it could prevent additional enumeration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With