Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The most straightforward thing I could think to do is create a <code>PrintJobByDepartmenReportItem</code> constructor that accepts a single <code>IEnumerable&lt;IGrouping&lt;string, PrintJob&gt;&gt;</code> parameter (which I believe should be the type of variable <code>g</code> in your sample). Keep in mind this also requires a parameter-less constructor definition, and your inherited classes would also need to implement a constructor prototype to call the base class constructor with the parameter:</p> <p><strong>Constructor</strong></p> <pre><code>public PrintJobReportItem() { } public PrintJobReportItem(IEnumerable&lt;IGrouping&lt;string, PrintJob&gt;&gt; g) { this.TotalPagesPrinted = g.Sum(i =&gt; i.GetEnumerator().Current.PagesPrinted); this.AveragePagesPrinted = g.Average(i =&gt; i.GetEnumerator().Current.PagesPrinted); this.PercentOfSinglePagePrintJobs = g.Count(i =&gt; i.GetEnumerator().Current.PagesPrinted == 1) * 100 / g.Count(i =&gt; i.GetEnumerator().Current.PagesPrinted &gt; 1); } </code></pre> <p><strong>Inherited Constructor</strong></p> <pre><code>public PrintJobByDepartmentReportItem(IEnumerable&lt;IGrouping&lt;string, PrintJob&gt;&gt; g) : base(g) { this.DepartmentName = g.First().Key; this.NumberOfUsers = g.Select(i =&gt; i.GetEnumerator().Current.UserName).Distinct().Count(); } </code></pre> <p><strong>Queries</strong></p> <pre><code>var q1 = repo.GetQuery&lt;PrintJob&gt;() .GroupBy(pj =&gt; pj.UserName) .Select(g =&gt; new PrintJobByUserReportItem(g)); var q2 = repo.GetQuery&lt;PrintJob&gt;() .GroupBy(pj =&gt; pj.Department) .Select(g =&gt; new PrintJobByDepartmentReportItem(g)); </code></pre> <p>This does have the one downside of assuming you will always be grouping by a string member, but you could presumably <code>GroupBy(i =&gt; i.MyProperty.ToString())</code> when appropriate or possibly change the prototype to accept <code>IEnumerable&lt;IGrouping&lt;object, PrintJob&gt;&gt;</code>.</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