C#使用linq計算執(zhí)行元素在列表中出現(xiàn)次數(shù)的方法

字號:


    具體如下:
    這是使用linq計算元素在列表中出現(xiàn)的次數(shù),調(diào)用方法非常簡單,和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());
    }