Split comma separated string to count duplicates
I have the following data in my database (comma separated strings):
"word, test, hello"
"test, lorem, word" "test" ... etcHow can I transform this data into a Dictionary whereby each string i开发者_如何学运维s separated into each distinct word together with the number of times that it occurs, i.e.
{"test", 3}, {"word", 2}, {"hello", 1}, {"lorem", 1}
I will have approximately 3000 rows of data in case this makes a difference to any solution offered. Also I am using .NET 3.5 (and would be interested to see any solution using linq)
IEnumerable<string> strings = ...;
Dictionary<string,int> result = strings.SelectMany(s => s.Split(','))
.GroupBy(s => s.Trim())
.ToDictionary(g => g.Key, g => g.Count());
Here is something like a pseudocode(haven't tried to compile it)
List<string> allRows = getFromDatabase();
var result = new Dictionary<string, int>();
foreach (string row in allRows)
{
string[] words = row.Split(',');
foreach (string word in words)
if (result.ContainsKey(word))
result[word]++;
else
result.Add(word, 1);
}
精彩评论