如何枚举Linq结果?

Sha*_*air 0 .net c# linq string

参考此主题:最小化LINQ字符串标记计数器 并使用以下提供的代码:

string src = "for each character in the string, take the rest of the " +
             "string starting from that character " +
             "as a substring; count it if it starts with the target string";

var results = src.Split()               // default split by whitespace
                 .GroupBy(str => str)   // group words by the value
                 .Select(g => new
                              {
                                  str = g.Key,      // the value
                                  count = g.Count() // the count of that value
                              });
Run Code Online (Sandbox Code Playgroud)

我需要枚举所有值(关键字和出现次数)并将它们加载到NameValueCollection中.由于我的林克知识非常有限,无法弄清楚如何制作它.请指教.谢谢.

mqp*_*mqp 5

我不会猜到你为什么要把任何东西放进去NameValueCollection,但是有一些原因

foreach (var result in results)
    collection.Add(result.str, result.count.ToString());
Run Code Online (Sandbox Code Playgroud)

还不够吗?

(编辑:更改了访问者Add,这可能更适合您的用例.)

如果答案是"不,那就有效",你应该停下来,弄清楚上面的代码在你的项目中使用它之前到底是做什么的.