//we load all discounts, and filter them using "discountType" parameter later (in memory)
//we do it because we know that this method is invoked several times per HTTP request with distinct "discountType" parameter
//that's why let's access the database only once
var discounts = (await _discountRepository.GetAllAsync(query =>
{
if (!showHidden)
query = query.Where(discount =>
(!discount.StartDateUtc.HasValue || discount.StartDateUtc <= DateTime.UtcNow) &&
(!discount.EndDateUtc.HasValue || discount.EndDateUtc >= DateTime.UtcNow));
//filter by coupon code
if (!string.IsNullOrEmpty(couponCode))
query = query.Where(discount => discount.CouponCode == couponCode);
//filter by name
if (!string.IsNullOrEmpty(discountName))
query = query.Where(discount => discount.Name.Contains(discountName));
query = query.OrderBy(discount => discount.Name).ThenBy(discount => discount.Id);
return query;
}, cache => cache.PrepareKeyForDefaultCache(NopDiscountDefaults.DiscountAllCacheKey,
showHidden, couponCode ?? string.Empty, discountName ?? string.Empty)))
.AsQueryable();
This piece of code load all types of discounts depending on the StartDateUtc,EndDateUtc,couponCode, and discountName from the database then keep that data in the cache(memory/distributed) then by doing AsQueryable it comes to memory and applies others filter like discountType, discountStatusId, etc later of the method.
Is it true or I misunderstood?