thestrayed thestrayed - 1 month ago 8
C# Question

Calculate IEnumerable<decimal> with decimal without using for-loop

I was given a discount percentage as a decimal and I have

IEnumerable<decimal>
as a list of prices. I want to calculate discount price for each one in linq.

IEnumerable<decimal> prices = new IEnumerable<decimal>() { 100, 200, 300};
decimal discountPercentage = 20;


Shoot me some idea, I would love to try anything.

Answer

Hope this helps

IEnumerable<decimal> prices = new List<decimal>() { 100, 200, 300};
decimal discountPercentage = 20;
var discounted = prices.Select(x=>x-x/100*discountPercentage).ToList();