tags:

views:

87

answers:

3

Referring to this topic: http://stackoverflow.com/questions/4038836/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.

+4  A: 

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.

mquander
Wouldn't you want to use collection.Add, since NameValueCollections can have multiple keys?
Ocelot20
Probably, yeah. I was unfamiliar with `NameValueCollections`. Fixed it.
mquander
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.
Sphynx
Gotcha. My pleasure.
mquander
+2  A: 

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);
Ocelot20
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
And now it has been fixed. Disregard previous comment up to "+1..."
diceguyd30
+1  A: 

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