2
你能帮我编写一个计算LINQ的字符串吗?喜欢这个?每个符号重复多少次
String text="aaabbcccdde";
Dictionary<int,char> result=....//LINQ
foreach (var t in result)
{
Console.WriteLine("Symbol {0} is met {1} times",t.symbol,t.times);
}
你能帮我编写一个计算LINQ的字符串吗?喜欢这个?每个符号重复多少次
String text="aaabbcccdde";
Dictionary<int,char> result=....//LINQ
foreach (var t in result)
{
Console.WriteLine("Symbol {0} is met {1} times",t.symbol,t.times);
}
你可以使用一个小的Linq:
var result = text.GroupBy(c => c)
.Select(g => new { symbol = g.Key, times = g.Count() });
foreach (var t in result)
{
Console.WriteLine("Symbol {0} is met {1} times",t.symbol,t.times);
}
或者更简单地说
var result = text.GroupBy(c => c, (c, g) => new { symbol = c, times = g.Count() });
foreach (var t in result)
{
Console.WriteLine("Symbol {0} is met {1} times",t.symbol,t.times);
}
String text = "aaabbcccdde";
foreach (var t in text.GroupBy(c => c))
{
Console.WriteLine("Symbol {0} is met {1} times", t.Key, t.Count());
}
哪里Key
是被分组和Count()
是次数炭出现Key
。