0

Referring to this topic: Minimize LINQ string token counter And using the following provided code:

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
                              });

I need to enumerate all values (keywords and number of occurrences) and load them into NameValueCollection. Due to my very limited Linq knowledge, can't figure out how to make it. Please advise. Thanks.

Community
  • 1
  • 1
SharpAffair
  • 5,558
  • 13
  • 78
  • 158

3 Answers3

5

I won't guess why you want to put anything in a NameValueCollection, but is there some reason why

foreach (var result in results)
    collection.Add(result.str, result.count.ToString());

is not sufficient?

(EDIT: Changed accessor to Add, which may be better for your use case.)

If the answer is "no, that works" you should probably stop and figure out what the hell the above code is doing before using it in your project.

mqp
  • 70,359
  • 14
  • 95
  • 123
  • Wouldn't you want to use collection.Add, since NameValueCollections can have multiple keys? – Ocelot20 Oct 29 '10 at 18:07
  • Probably, yeah. I was unfamiliar with `NameValueCollections`. Fixed it. – mqp Oct 29 '10 at 18:10
  • 1
    I perfectly understand what the above code is doing, but most of the time I use a different .NET language, so I'm new to both C# and Linq :) Didn't think it was so simple, thanks. – SharpAffair Oct 29 '10 at 18:13
2

Looks like your particular problem could just as easily use a Dictionary instead of a NameValueCollection. I forget if this is the correct ToDictionary syntax, but just google the ToDictionary() method:

Dictionary<string, int> useADictionary = results.ToDictionary(x => x.str, x => x.count);
Greg
  • 23,155
  • 11
  • 57
  • 79
Ocelot20
  • 10,510
  • 11
  • 55
  • 96
  • The second argument in your call is an assignment instead of an anonymous function, but +1 for suggesting a dictionary instead of a NameValueCollection! – diceguyd30 Oct 29 '10 at 18:15
  • And now it has been fixed. Disregard previous comment up to "+1..." – diceguyd30 Oct 29 '10 at 18:26
1

You certainly want a Dictionary instead of NameValueCollection. The whole point is to show unique tokens (strings) with each token's occurrence count (an int), yes?

NameValueCollection is a special-purpose collection that requires string and key and value - Dictionary<string, int> is the mainstream .Net way to associate a unique string key with its corresponding int value.

Take a look at the various System.Collections namespaces to understand what each is intended to achieve. Typically these days, System.Collections.Generic is the most widely-seen, with System.Collections.Concurrent for multithreaded programs.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140