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 6477609
In Process

The Archive Base Latest Questions

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

This is pretty simple but I’m at a loss: Given this type of data

  • 0

This is pretty simple but I’m at a loss:
Given this type of data set:

UserInfo(name, metric, day, other_metric)

and this sample data set:

joe  1 01/01/2011 5
jane 0 01/02/2011 9
john 2 01/03/2011 0
jim  3 01/04/2011 1
jean 1 01/05/2011 3
jill 2 01/06/2011 5
jeb  0 01/07/2011 3
jenn 0 01/08/2011 7

I’d like to retrieve a table that lists metrics in order(0,1,2,3..) with the total number of times the count occurs. So from this set, you’d end up with:

0 3    
1 2    
2 2    
3 1

I’m grappling with the LINQ syntax but am stuck on where to put a groupby and count…
any help?

POST Edit: I was never able to get the posted answers to work as they always returned one record with the number of different counts. However, I was able to put together a LINQ to SQL example that did work:

var pl = from r in info
         orderby r.metric    
         group r by r.metric into grp
         select new { key = grp.Key, cnt = grp.Count()};

This result gave me an ordered set of records with ‘metrics’ and the number of users associated with each. I’m clearly new to LINQ in general and to my untrained eye this approach seems very similar to the pure LINQ approach yet gave me a different answer.

  • 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-25T06:58:10+00:00Added an answer on May 25, 2026 at 6:58 am

    After calling GroupBy, you get a series of groups IEnumerable<Grouping>, where each Grouping itself exposes the Key used to create the group and also is an IEnumerable<T> of whatever items are in your original data set. You just have to call Count() on that Grouping to get the subtotal.

    foreach(var line in data.GroupBy(info => info.metric)
                            .Select(group => new { 
                                 Metric = group.Key, 
                                 Count = group.Count() 
                            })
                            .OrderBy(x => x.Metric))
    {
         Console.WriteLine("{0} {1}", line.Metric, line.Count);
    }
    

    > This was a brilliantly quick reply but I’m having a bit of an issue with the first line, specifically “data.groupby(info=>info.metric)”

    I’m assuming you already have a list/array of some class that looks like

    class UserInfo {
        string name;
        int metric;
        ..etc..
    } 
    ...
    List<UserInfo> data = ..... ;
    

    When you do data.GroupBy(x => x.metric), it means "for each element x in the IEnumerable defined by data, calculate it’s .metric, then group all the elements with the same metric into a Grouping and return an IEnumerable of all the resulting groups. Given your example data set of

        <DATA>           | Grouping Key (x=>x.metric) |
    joe  1 01/01/2011 5  | 1
    jane 0 01/02/2011 9  | 0
    john 2 01/03/2011 0  | 2
    jim  3 01/04/2011 1  | 3
    jean 1 01/05/2011 3  | 1
    jill 2 01/06/2011 5  | 2
    jeb  0 01/07/2011 3  | 0
    jenn 0 01/08/2011 7  | 0
    

    it would result in the following result after the groupby:

    (Group 1): [joe  1 01/01/2011 5, jean 1 01/05/2011 3]
    (Group 0): [jane 0 01/02/2011 9, jeb  0 01/07/2011 3, jenn 0 01/08/2011 7]
    (Group 2): [john 2 01/03/2011 0, jill 2 01/06/2011 5]
    (Group 3): [jim  3 01/04/2011 1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im sure this is pretty simple but I just cant seem to find the
This is a pretty simple question but I'm somewhat stumped. I am capturing sections
this is probably pretty simple, but I've got this text file containing a bunch
This should be pretty simple, but I am new at LINQ. I have a
This is probably pretty simple, but I can't figure out how to do it:
This feels like it should be pretty simple, but not much seems to be
I'm pretty sure this is a simple question in regards to formatting but here's
This is probably pretty simple. I want to select all elements of a given
this should be pretty simple but I don't get it. How can I draw
This should be pretty simple but it's not working. I have a file underneath

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.