0

我想计算列表包含字符串的计数。

我不想要列表计数。我不想要列表的特定字符串计数。

我只想要列表中包含特定字符串的项目数。

在 Linq 或普通 c# 中以任何方式

 Hashtable newlist  = new Hashtable();
      newlist = System.Web.HttpContext.Current.Application["Userlogin"] as Hashtable;
count = newlist.Count(s => newlist.Contains(sKey));

foreach (DictionaryEntry item1 in newlist)
{
    if (newlist.Contains(sKey))
    {
         count = count + 1;
    }
}

newlist 是一个哈希表

4

3 回答 3

2

据我所知你的问题

List<string> list = new List<string>() { "a","b","a","c","c" };

var counts = list.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());

var cntC = counts["c"];
var cntA = counts["a"];
于 2012-12-14T08:54:09.803 回答
0

怎么样 :

count = newlist.AsQueryable().Where(s => s.Contains(sKey)).Count();

我是否正确理解你想要得到的东西?

于 2012-12-14T08:50:41.567 回答
0

类似的东西

var newlist = new string[5] {"qqq","wwqww","wqwww","wqwww","wqwwxw"};

var count = newlist.Count(s => s.Contains("x"));

输出

count = 1
于 2012-12-14T08:55:50.617 回答