C#使用linq计算执行元素在列表中出现次数的方法
更新时间:2015年04月06日 16:51:11 作者:令狐不聪
这篇文章主要介绍了C#使用linq计算执行元素在列表中出现次数的方法,涉及C#使用linq扩展进行列表查询的技巧,需要的朋友可以参考下
本文实例讲述了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#程序设计有所帮助。
您可能感兴趣的文章:
相关文章
C# 中const,readonly,static的使用小结
这篇文章主要介绍了C# 中使用const,readonly,static的示例,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下2021-01-01
最新评论