I have a StringCollection object with 5 words in them. 3 of them are duplicate words. I am trying to create a LINQ query that will count how many unique words are in the collection and output them to to the console. So, for example, if my StringCollection has 'House', 'Car', 'House','Dog', 'Cat', then it should output like this:
House --> 2 Car --> 1 Dog --> 1 Cat --> 1
Any ideas on how to create a LINQ query to do this?
Try the following
var res = from word in col.Cast<string>()
group word by word into g
select new { Word = g.Key, Count = g.Count() };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With