多少次重复每个符号

use*_*406 2 c# linq string

你能帮我写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);
}
Run Code Online (Sandbox Code Playgroud)

p.s*_*w.g 6

你可以使用一点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);
}
Run Code Online (Sandbox Code Playgroud)

或者更简单

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);
}
Run Code Online (Sandbox Code Playgroud)