你需要一个Select
后来GroupBy
获得你需要的结果:
var result = text .ToUpper() .Where(char.IsLetter) .GroupBy(c => c) .Select(g => new { Letter = g.Key, Count = g.Count() });
然后,"a: 56 b: 34 c: 53"
您可以使用string.Join
和格式化结果string.Format
:
string formattedResult = string.Join(" ", result.Select(x => string.Format("{0}: {1}", x.Letter, x.Count)));