When the priceList is small the query is executed quickly, but if the priceList has more than five elements the query is very slow.
if(!filters.Price.All && !filters.Price.IsEmpty())
{
var priceList = filters.Price.GetRangeList();
foreach (var price in priceList)
{
decimal startPrice = decimal.Parse(price[0]);
decimal endPrice = decimal.Parse(price[1]);
var priceResult = from deal in query
where (deal.DiscountPrice >= startPrice && deal.DiscountPrice <= endPrice)
select deal;
if(priceResult.Count() != 0)
priceResults = (priceResults == null) ? priceResult : priceResult.Union(priceResults);
}
query = query.Intersect(priceResults);
}
I may be misunderstanding your code, but as far as I can tell, the result of the Intersect operation will have same the list of objects as priceResults – hence the Intersect is completely unnecessary (so removing it will be quite an optimization). 🙂
Unless the priceResults list has contents before entering this code block, it will only have objects from the query list, so finding the intersection between the two will always result in the priceResults list.