本文整理汇总了C#中Stack.GroupBy方法的典型用法代码示例。如果您正苦于以下问题:C# Stack.GroupBy方法的具体用法?C# Stack.GroupBy怎么用?C# Stack.GroupBy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Stack
的用法示例。
在下文中一共展示了Stack.GroupBy方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RunLINQ
public static void RunLINQ()
{
Stack<MergeField> _index = new Stack<MergeField>();
_index.Push(new MergeField() { index = 1, name = "I" });
_index.Push(new MergeField() { index = 2, name = "I" });
_index.Push(new MergeField() { index = 3, name = "F" });
_index.Push(new MergeField() { index = 4, name = "M" });
_index.Push(new MergeField() { index = 5, name = "I" });
_index.Push(new MergeField() { index = 6, name = "L" });
_index.Push(new MergeField() { index = 7, name = "I" });
_index.Push(new MergeField() { index = 8, name = "K" });
_index.Push(new MergeField() { index = 9, name = "O" });
_index.Push(new MergeField() { index = 10, name = "I" });
//IEnumerable<MergeField>
var groups = _index.GroupBy(mf => mf.name).Where(grp => grp.Count() > 1).Select(grp => grp.Key);
var group = _index.GroupBy(mf => mf.name).Where(grp => grp.Count() == 1).Select(grp => grp.First()).OrderByDescending(mf => mf.index); ;
foreach (MergeField m in group)
{
Console.WriteLine(m.index.ToString() + " : " + m.name);
}
Console.WriteLine("Flags");
}
示例2: Calculate
/// <summary>
/// Calculates change
/// </summary>
/// <param name="money">input amount of money - dictionary where key - denomination, value - units amount</param>
/// <param name="sum">sum that should be composed</param>
/// <returns>dictionary where key - denomination and value - amount</returns>
public IDictionary<decimal, int> Calculate(IDictionary<decimal, int> money, decimal sum)
{
if (money == null)
{
throw new ArgumentNullException("money");
}
var balance = new Balance(money);
var result = new Stack<decimal>();
var currentSum = 0m;
var denomination = balance.PopTheSameOrLess(balance.GetMaxAvailable());
while (currentSum != sum)
{
if (denomination == 0 && result.Count == 0)
{
break;
}
if (currentSum + denomination <= sum && denomination > 0)
{
result.Push(denomination);
currentSum += denomination;
denomination = balance.PopTheSameOrLess(denomination);
}
else if (denomination > 0)
{
balance.Push(denomination);
denomination = balance.PopLessThat(denomination);
}
else
{
while (denomination == 0 && result.Count > 0)
{
var previous = result.Pop();
currentSum -= previous;
balance.Push(previous);
denomination = balance.PopLessThat(previous);
}
}
}
return result.GroupBy(item => item).ToDictionary(g => g.Key, g => g.Count());
}