Finding count of a item in List using LINQ

This is my new post in extension to previous one of finding count of a item in ArrayList using LINQ which can be found here , in this post we will see how to get count of particular item in a List using LINQ.

The method to get count:

public int GetCountInList<T>(IEnumerable<T> list, T key)
{
    int iCount = 0;
    iCount=list.Count(item => EqualityComparer<T>.Default.Equals(item, key));
    return iCount;
}

Use of the method :

List<int> lstNos = new List<int>();
lstNos.Add(1);
lstNos.Add(2);
lstNos.Add(1);
lstNos.Add(3);
lstNos.Add(2);
lstNos.Add(2);

int i = 0;
i=GetCountInList<int>(lstNos, 1);
Console.WriteLine("Count of 1 is = " + i);

List<string> lstNames = new List<string>();
lstNames.Add("Jhon");
lstNames.Add("Jhon");
lstNames.Add("Bob");
lstNames.Add("Mike");
lstNames.Add("Jhon");
lstNames.Add("Mike");

i = GetCountInList<string>(lstNames, "Jhon");
Console.WriteLine();
Console.WriteLine("Count of Jhon is = " + i);

Console.ReadLine();

List<int> lstNos = new List<int>();
lstNos.Add(1);
lstNos.Add(2);
lstNos.Add(1);
lstNos.Add(3);
lstNos.Add(2);
lstNos.Add(2);

int i = 0;
i=GetCountInList<int>(lstNos, 1);
Console.WriteLine("Count of 1 is = " + i);

List<string> lstNames = new List<string>();
lstNames.Add("John");
lstNames.Add("John");
lstNames.Add("Bob");
lstNames.Add("Mike");
lstNames.Add("John");
lstNames.Add("Mike");

i = GetCountInList<string>(lstNames, "John");
Console.WriteLine();
Console.WriteLine("Count of Jhon is = " + i);

Console.ReadLine();

Output :

Count of 1 is = 2

Count of John is =  3

Note:

    The above method works with almost all traditional data types.



Deven Sawant

Published Tuesday, September 21, 2010 1:19 PM by devensawant
Filed under: , , ,

Comments

# re: Finding count of a item in List using LINQ

Tuesday, September 21, 2010 9:45 AM by Andy

Why instantiate a variable at all?

return list.Count(item => EqualityComparer<T>.Default.Equals(item, key));

# re: Finding count of a item in List using LINQ

Wednesday, September 22, 2010 4:37 AM by shinobi

Whats wrong with:

int countOfJohn = lstName.Count(item => item == "John")

Why write something with 10 line when 1 will do it better.

# re: Finding count of a item in List using LINQ

Wednesday, September 22, 2010 10:17 AM by Nick

This is a minor nitpick, but wouldn't it make more sense to turn this into an Extension method? I know some people think they are over used, but I think this is a good application. The calling syntax is just a lot easier since the types can all be inferred.

public static class Extensions

{

   public static int GetCountInList<T>(this IEnumerable<T> list, T key)

   {

       return list.Count(item => EqualityComparer<T>.Default.Equals(item, key));

   }

}

// ...

i = lstNos.GetCountInList(1);

Console.WriteLine("Count of 1 is = " + i);

# re: Finding count of a item in List using LINQ

Wednesday, September 22, 2010 12:33 PM by Brian Low

A couple of ideas:

- make it extension method on IEnumerable<T> for better readability

- accept a Predicate<T> for more flexibility

i = lstNames.CountOf(name => name == "Jhon")

# re: Finding count of a item in List using LINQ

Wednesday, September 22, 2010 4:14 PM by BEM

I'd be inclined to do something like this instead:

lstNames.Where(name => name == "John").Count();

# re: Finding count of a item in List using LINQ

Friday, September 24, 2010 2:16 AM by devensawant

Good suggestion Nick i will write a new post for creating extension method for the same

Leave a Comment

(required) 
(required) 
(optional)
(required)