在使用LINQ收集统计单词 [英] Counting words in a collection using LINQ

查看:256
本文介绍了在使用LINQ收集统计单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个字在其中一个StringCollection对象。其中3人是重复的话。我想创建一个LINQ查询,将计算有许多独特的话是收集并输出到控制台。因此,举例来说,如果我的StringCollection有家​​,汽车,房子,狗,猫,那么就应该像这样的输出:

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

如何创建LINQ查询要做到这一点任何想法?

Any ideas on how to create a LINQ query to do this?

推荐答案

请尝试以下

var res = from word in col.Cast<string>()
          group word by word into g
          select new { Word = g.Key, Count = g.Count() };

这篇关于在使用LINQ收集统计单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆