我的数据库中有以下数据(以逗号分隔的字符串):
"word,test,hello"
"test,lorem,word"
"test"
......
等等
如何将此数据转换为字典,其中每个字符串被分成每个不同的单词以及它出现的次数,即
{"test", 3}, {"word", 2}, {"hello", 1}, {"lorem", 1}
Run Code Online (Sandbox Code Playgroud)
我会有大约3000行数据,以防这对所提供的任何解决方案产生影响.我也使用.NET 3.5(并有兴趣看到使用linq的任何解决方案)
IEnumerable<string> strings = ...;
Dictionary<string,int> result = strings.SelectMany(s => s.Split(','))
.GroupBy(s => s.Trim())
.ToDictionary(g => g.Key, g => g.Count());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1893 次 |
| 最近记录: |