Click here to Skip to main content
12,302,493 members (65,351 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

13.5K views
3 bookmarked
Posted

Count number of occurences of a value in a List using LINQ

, 29 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Did you ever face a situation where you want to calculate total number of occurrences of a value in a List object?What approach did you used for it?Did you ever try LINQ for the same? Yes, you can use it. Just look at the example below:class Program{ static void Main() ...
Did you ever face a situation where you want to calculate total number of occurrences of a value in a List object?

What approach did you used for it?

Did you ever try LINQ for the same? Yes, you can use it. Just look at the example below:

class Program
{
    static void Main()
    {
        List<int> list = new List<int>();
        list.AddRange(new int[] { 1, 2, 3, 2, 1, 4, 5, 1, 7, 1, 8, 1});
        Console.WriteLine("1 occurs {0} times.", CountOccurenceOfValue(list, 1));
        Console.WriteLine("1 occurs {0} times.", CountOccurenceOfValue2(list, 1));
    }
 
    static int CountOccurenceOfValue(List<int> list, int valueToFind)
    {
        return ((from temp in list where temp.Equals(valueToFind) select temp).Count());
    }
 
    static int CountOccurenceOfValue2(List<int> list, int valueToFind)
    {
        int count = list.Where(temp => temp.Equals(valueToFind))
                    .Select(temp => temp)
                    .Count();
        return count;
    }
}


Hope this will help Smile | :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

The Manoj Kumar
Team Leader
India India

Manoj Kumar is a Humble Programmer and a trainer having dozens of trainings, publications and articles to his wallet.

His programming adventures began with Basic at an age of 11. Being a mathematician at core, soon he started looking for some more and moved to assembly language, later to C, C++, VC++ and finally to .Net.

He started his professional career as a VC++ 6 trainer, moved to embedded systems and device driver development, then to complex biological systems and finally moved to pure application development.

He has been teaching and training people for more than 12 years on a wide range of topics including Mathematics, Algorithms, Data Structures, C, C++, VC++, MFC, C#, Design Patterns and now a days he is working extensively with Visual Studio and .Net framework which includes VSX, WPF, Silverlight, WCF, WF, XAML and RIAs.

Awards:

  • Ideablade RIA Service Challenge winner
  • Visual Studio 2010 Extension Contest winner (Best Use of Editor)

Visit my website and blog or drop me a mail.

Feel free to connect with me on Linkedin.


You may also be interested in...

Comments and Discussions

 
Suggestion[My vote of 2] Better oneliner Pin
Mikkel Lund26-Mar-16 2:22
memberMikkel Lund26-Mar-16 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160530.1 | Last Updated 29 Mar 2010
Article Copyright 2010 by The Manoj Kumar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid