简体   繁体   中英

Dynamic LINQ Query calculate percentage withing the LINQ C#

I have an e-commerce which i also want to do product filtering using Price From & Price To respectively. The problem that I currently have is that the price in my DB is not the final price shown to the customer. The price in DB is changed on run-time as we add our PG Fee and our commission fee as well. Our commission fee is based on the product category, so we take the product category and make the calculation.

Now here is the problem, using Dynamic LINQ Query has been giving me difficulties, because if i make my filtration like below:

query = query.Where(x => x.Price >= getProductsDomainModel.PriceFrom && x.Price <= getProductsDomainModel.PriceTo);

It will filter on the DB price but then it calculates the run-time price after this filtration which will show wrong results to the client. If the commission was a standard price then it would be easy, i would just add the calculation withing the LINQ but now its also based on the product category.

Any help?

You may do something like:

query = query.Where(x => PriceAfterCommission(x) > getProductsDomainModel.PriceFrom && PriceAfterCommission(x) <= getProductsDomainModel.PriceTo)

decimal PriceAfterCommission(Product x)
{
  decimal commission = CommissionByCategory(x);
  return x.Price + commission;
}

In ComissionByCategory you will do the calculation of the commission based on the product category.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM