Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6607095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:27:58+00:00 2026-05-25T19:27:58+00:00

I asked a question before on retrieving the count of data server side and

  • 0

I asked a question before on retrieving the count of data server side and was provided a solution on the site. The suggestion was to use linq which worked perfectly however since i’m relatively new to it I need a little in depth help.

Using John’s solution:

class Guy
{
    public int age; public string name;
    public Guy( int age, string name ) {
        this.age = age;
        this.name = name;
    }

}

class Program
{
    static void Main( string[] args ) {
        var GuyArray = new Guy[] { 
        new Guy(22,"John"),new Guy(25,"John"),new Guy(27,"John"),new Guy(29,"John"),new Guy(12,"Jack"),new Guy(32,"Jack"),new Guy(52,"Jack"),new Guy(100,"Abe")};

    var peeps = from f in GuyArray group f by f.name into g select new { name = g.Key, count = g.Count() };

        foreach ( var record in peeps ) {
            Console.WriteLine( record.name + " : " + record.count );
        }

    }
}

I can get the count of occurrences of John, Jake and Abe using the above as suggested by John. However what if the problem was a little more complicated for instance

var GuyArray = new Guy[] { 
new Guy(22,"John", "happy"),new Guy(25,"John", "sad"),new Guy(27,"John", "ok"),
new Guy(29,"John", "happy"),new Guy(12,"Jack", "happy"),new Guy(32,"Jack", "happy"),
new Guy(52,"Jack", "happy"),new Guy(100,"Abe", "ok")};

The above code works great to retrieve the number of occurrences of the different names but what if I need the number of occurences of the names and also the number of occurrences of each person who is happy, sad or ok as well. ie output is : Name, count of names, count of names that are happy, count of names that are sad, count of names that are ok. If linq isn’t the best solution for this, I am prepared to listen to all alternatives. Your help is much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:27:58+00:00Added an answer on May 25, 2026 at 7:27 pm

    Frankly, it’s not clear if you want the total number of people that are happy, or the total number of people that are happy by name (also for sad, ok). I’ll give you a solution that can give you both.

    var nameGroups = from guy in GuyArray 
                     group guy by guy.name into g
                     select new { 
                         name = g.Key,
                         count = g.Count(),
                         happy = g.Count(x => x.status == "happy"),
                         sad = g.Count(x => x.status == "sad"),
                         ok = g.Count(x => x.status == "ok")
                     };
    

    Then:

    foreach(nameGroup in nameGroups) {
        Console.WriteLine("Name = {0}, Count = {1}, Happy count = {2}, Sad count = {3}, Okay count = {4}", nameGroup.name, nameGroup.count, nameGroup.happy, nameGroup.sad, nameGroup.ok);
    }
    

    If you want the total happy, sad, ok counts you can say:

    Console.WriteLine(nameGroups.Sum(nameGroup => nameGroup.happy));
    

    etc.

    Additionally, you should make an enum

    public enum Mood {
        Happy,
        Sad,
        Okay
    }
    

    and then

    class Guy {
        public int Age { get; set; }
        public string Name { get; set; }
        public Mood Mood { get; set; }
    }
    

    so that you can instead write:

    var people = from guy in guyArray 
                 group guy by guy.Name into g
                 select new { 
                     Name = g.Key,
                     Count = g.Count(),
                     HappyCount = g.Count(x => x.Mood == Mood.Happy),
                     SadCount = g.Count(x => x.Mood == Mood.Sad),
                     OkayCount = g.Count(x => x.Mood == Mood.Okay)
                 };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've asked this question before, here , however that solution didn't fix it when
I asked this question before, but had a hard time showing which edits I
I asked this question before, Here however I think I presented the problem poorly,
I asked this question before and got a great working answer. what is the
I have asked this question before and the problem was half solved in the
I asked a similar question before only to later discover that what I thought
I have asked a similar question before, but I didn't have a firm grasp
This has been asked before (question no. 308581) , but that particular question and
I asked this sort of question before ( Application fails to dynamically _re_load JavaScript
I'm new to jquery, and sorry if this question is asked before (could find

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.