我有一个带有以下签名的字典:
Dictionary<decimal, int>
里面的项目看起来像这样:
90, 2
60, 3
30, 4
20, 1
我需要将其扩展为具有以下特征的列表:
List<double>
里面的项目看起来像这样:
90
90
60
60
60
30
30
30
30
20
有没有什么优雅高效的方法来实现这个?
我有一个带有以下签名的字典:
Dictionary<decimal, int>
里面的项目看起来像这样:
90, 2
60, 3
30, 4
20, 1
我需要将其扩展为具有以下特征的列表:
List<double>
里面的项目看起来像这样:
90
90
60
60
60
30
30
30
30
20
请尝试以下方法:
dictionary
.SelectMany(pair => Enumerable.Repeat((double)pair.Key, pair.Value))
.OrderByDescending(x => x)
.ToList();
dictionary
.SelectMany(kvp => Enumerable.Repeat(Decimal.ToDouble(kvp.Key), kvp.Value))
.ToList()
foreach (var pair in dict)
for (int i=0;i<pair.Value;i++)
list.Add((double)pair.Key);
public static IEnumerable<T> Blowout<T>(T value, int length)
{
for (int i = 0; i < length; i++) yield return value;
}
dictionary
.SelectMany(pair => Blowout((double)pair.Key, pair.Value));