我给出了折扣百分比作为小数,并且我有IEnumerable<decimal>
作为价格列表。我想要计算linq中每个人的折扣价格。使用十进制计算IEnumerable <decimal>而不使用for循环
IEnumerable<decimal> prices = new IEnumerable<decimal>() { 100, 200, 300};
decimal discountPercentage = 20;
给我一些想法,我很乐意尝试任何事情。
我给出了折扣百分比作为小数,并且我有IEnumerable<decimal>
作为价格列表。我想要计算linq中每个人的折扣价格。使用十进制计算IEnumerable <decimal>而不使用for循环
IEnumerable<decimal> prices = new IEnumerable<decimal>() { 100, 200, 300};
decimal discountPercentage = 20;
给我一些想法,我很乐意尝试任何事情。
希望这有助于
IEnumerable<decimal> prices = new List<decimal>() { 100, 200, 300};
decimal discountPercentage = 20;
var discounted = prices.Select(x=>x-x/100*discountPercentage).ToList();
var discountPercentage = 20;
var discounted = prices.Select(p => p -= p * (discountPercentage/100)).ToArray();
不能初始化IEnumerable
。使用List
代替
new List<decimal> { 100, 200, 300 }.
Select(value => value * (100 - discountPercentage)/100);
@fubo东西真的不好用我的计算:d –
如果这是关于折扣价,你不得不从。减去你的价格折扣。
因此,从100
折扣20%
将导致折扣价为80
。
IEnumerable<decimal> prices = new List<decimal>() { 100m, 200m, 300m };
decimal discountPercentage = 20;
IEnumerable<decimal> discPrices = prices.Select(x => x/100 * (100 - discountPercentage));
// {80,160,240}
使用选择并列出您的结果。
prices.Select(value => value - value * discountPercentage /100).ToList();
请注意,您不能实例化IEnumerable,因为它是一个接口,而不是具体的类。所以,请使用列表并为其添加值。
一个建议的回答,
IEnumerable<decimal> prices = new List<decimal>() { 100, 200, 300 };
decimal discountPercentage = 20;
// calculate prices after applying the discount via LINQ
prices = prices.Select(p => (100 - discountPercentage)/100 * p);
// displaying for testing purposes
foreach (decimal price in prices)
{
Console.WriteLine(price);
}
这将显示计算的折扣价格如下图所示,
80.0 160.0 240.0
这是非常简单的LINQ。只需使用'.Select'。我们可以给你答案(和人们可能会),但如果是的话,你会如何学习? –
@GiladGreen确实是这样,我通常使用。选择但不知何故,我完全忘了,我们就可以访问它。今天感觉embarress – thestrayed