This article describes the method of C# using linq to calculate the number of times an execution element appears in a list. Share it for your reference. The details are as follows:
This is to use linq to calculate the number of times an element appears in the list. The call method is very simple and very similar to the SQL statement.
Copy the codeThe code is as follows:
// 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 (searchItem) select t).Count());
}
public static int CountTimes<T>(this List<T> inputList, T searchItem)
{
return ((from t in inputList where (searchItem) select t).Count());
}
I hope this article will be helpful to everyone's C# programming.