Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After calling <code>GroupBy</code>, you get a series of groups <code>IEnumerable&lt;Grouping&gt;</code>, where each Grouping itself exposes the <code>Key</code> used to create the group and also is an <code>IEnumerable&lt;T&gt;</code> of whatever items are in your original data set. You just have to call <code>Count()</code> on that Grouping to get the subtotal.</p> <pre><code>foreach(var line in data.GroupBy(info =&gt; info.metric) .Select(group =&gt; new { Metric = group.Key, Count = group.Count() }) .OrderBy(x =&gt; x.Metric) { Console.WriteLine("{0} {1}", line.Metric, line.Count); } </code></pre> <p><hr /></p> <blockquote> <p>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)"</p> </blockquote> <p>I'm assuming you already have a list/array of some <code>class</code> that looks like</p> <pre><code>class UserInfo { string name; int metric; ..etc.. } ... List&lt;UserInfo&gt; data = ..... ; </code></pre> <p>When you do <code>data.GroupBy(x =&gt; x.metric)</code>, it means "for each element <code>x</code> in the IEnumerable defined by <code>data</code>, calculate it's <code>.metric</code>, then group all the elements with the same metric into a <code>Grouping</code> and return an <code>IEnumerable</code> of all the resulting groups. Given your example data set of </p> <pre><code> &lt;DATA&gt; | Grouping Key (x=&gt;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 </code></pre> <p>it would result in the following result after the groupby:</p> <pre><code>(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] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload