I need to fetch data based on below business requirement. We launch campaign's on items that we sell, which give the customer some discounts etc. We sell items of different brands, each brand has multiple products and each product has multiple models.
In the campaign table, if the campaign is on one specific model among the items, then a record is inserted with proper brandid of that model, productid of that model and model id too. If the campaign is on all models of a product, then a record is inserted with proper brandid ad productid and modelid as 0(which means applicable on all models of that product). If the campaign is on all models and products of a brand, then a record is inserted with proper brandid with productid, modelid as 0(which means applicable on all products and models of that brand).
Previously we were having the logic in stored procedure where we would check the count on modelid and if count is 0, then checked on product id with modelid as 0, and then at brand level with productid, modelid as 0. wherever the count is > 0, we would fetch the corresponding data.
Now we have moved this logic to business layer using linq queries. I have placed the campaign data for each brandid in cache. So cahce would be cache_brandid. I would fetch the brand specific data from cache and then check on modelid, if no data found then with product id with modelid as 0, and again if no data found then at brand id with productid, modelid as 0. However I am looking for more cleaner way of doing this, as the data I cache and apply the logic from this data from cache is more when compared to what I used to fetch from DB previously using SP. for each such data fetch now I am fetching many records from cache and iterating through them till I get the data, however SP would just check the count which is faster than fetching the data and would fetch only 1 record when it finds the count to be > 0
My current code looks like:
cacheList.FirstOrDefault(x=>x.brandid = b && x.productid == p && x.modelid == m && x.modelyear == myr)
??
cacheList.FirstOrDefault(x=>x.brandid == b && x.productid == p && x.modelid == m && x.modelyear == 0)
??
cacheList.FirstOrDefault(x=>x.brandid == b && x.productid == p && x.modelid == 0 && x.modelyear == myr)
??
cacheList.FirstOrDefault(x=>x.brandid == b && x.productid == p && x.modelid == 0 && x.modelyear == 0)
??
cacheList.FirstOrDefault(x=>x.brandid == b && x.productid == 0 && x.modelid == 0 && x.modelyear = 0);
Any assistance in making my approach cleaner and faster would greatly help me.
Thanks