Note that there are some explanatory texts on larger screens.

plurals
  1. POMimic `rollup` function on generic list
    text
    copied!<p>I have a generic list of type <code>Element</code>, for example.</p> <pre><code>public class Element { public string Country { get; set; } public string City { get; set; } public int Population { get; set; } } </code></pre> <p>With the following data.</p> <pre><code>var elements = new List&lt;Element&gt; { new Element { Country = "Country A", City = "Barrie", Population = 12 }, new Element { Country = "Country A", City = "Barrie2", Population = 12 }, new Element { Country = "Country A", City = "Barrie2", Population = 12 }, new Element { Country = "Country A", City = "Barrie", Population = 12 }, new Element { Country = "Country D", City = "Essex", Population = 12 }, new Element { Country = "Country A", City = "Barrie", Population = 12 }, new Element { Country = "Country A", City = "Barrie", Population = 12 }, new Element { Country = "Country D", City = "Essex", Population = 12 }, new Element { Country = "Country A", City = "Barrie", Population = 12 }, new Element { Country = "Country A", City = "Barrie", Population = 12 } }; </code></pre> <p>Essentially, I'd like a running total of the population grouped by country and city. </p> <p>Something like.</p> <pre><code>Country A | Barrie | `running total for Barrie` Country A | Barrie2 | `running total for Barrie2` | | `total for Country A` Country D | Essex | `running total for Essex` | | `total for Country D` | | `total for everything` </code></pre> <p>I couldn't find an extension (I say extension because I plan on using a rollup several times) anywhere so I figure I'd give it a shot. So I started with this simple query.</p> <pre><code>var groupedElements = elements .GroupBy(x =&gt; new { x.Country, x.City }) .Select(x =&gt; new { Country = x.Key, City = x.Select(xx =&gt; xx.City), Population = x.Sum(xx =&gt; xx.Population) }) .ToList(); </code></pre> <p>This query works as expected so I think I'm on the right track. Next I think I have to figure out which property from <code>groupedElements</code> is aggregate because that's what we'll be doing a rollup on. How do I accomplish that? Or perhaps I could have a parameter that makes me specify what column I wish to do the aggregate function on.</p>
 

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