C#使用linq计算执行元素在列表中出现次数的方法
内容摘要
本文实例讲述了C#使用linq计算执行元素在列表中出现次数的方法。分享给大家供大家参考。具体如下:
这是使用linq计算元素在列表中出现的次数,调用方法非常简单,和sql语句很像
这是使用linq计算元素在列表中出现的次数,调用方法非常简单,和sql语句很像
文章正文
本文实例讲述了C#使用linq计算执行元素在列表中出现次数的方法。分享给大家供大家参考。具体如下:
这是使用linq计算元素在列表中出现的次数,调用方法非常简单,和sql语句很像
复制代码 代码如下:
// Count the number of times an item appears in this list
public static int CountTimes<T>(this List<T> inputList, T searchItem)
{
return ((from t in inputList where t.Equals(searchItem) select t).Count());
}
public static int CountTimes<T>(this List<T> inputList, T searchItem)
{
return ((from t in inputList where t.Equals(searchItem) select t).Count());
}
希望本文所述对大家的C#程序设计有所帮助。
代码注释