Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When you use <strong>group by</strong> Linq creates a new collection of items so you have two collections of items.</p> <p>Here's a solution to both problems:</p> <ol> <li>summing any amount of members in one iteration and</li> <li>avoid duplicating your item's collection</li> </ol> <p>Code:</p> <pre><code>public static class LinqExtensions { /// &lt;summary&gt; /// Computes the sum of the sequence of System.Double values that are obtained /// by invoking one or more transform functions on each element of the input sequence. /// &lt;/summary&gt; /// &lt;param name="source"&gt;A sequence of values that are used to calculate a sum.&lt;/param&gt; /// &lt;param name="selectors"&gt;The transform functions to apply to each element.&lt;/param&gt; public static double[] SumMany&lt;TSource&gt;(this IEnumerable&lt;TSource&gt; source, params Func&lt;TSource, double&gt;[] selectors) { if (selectors.Length == 0) { return null; } else { double[] result = new double[selectors.Length]; foreach (var item in source) { for (int i = 0; i &lt; selectors.Length; i++) { result[i] += selectors[i](item); } } return result; } } /// &lt;summary&gt; /// Computes the sum of the sequence of System.Decimal values that are obtained /// by invoking one or more transform functions on each element of the input sequence. /// &lt;/summary&gt; /// &lt;param name="source"&gt;A sequence of values that are used to calculate a sum.&lt;/param&gt; /// &lt;param name="selectors"&gt;The transform functions to apply to each element.&lt;/param&gt; public static double?[] SumMany&lt;TSource&gt;(this IEnumerable&lt;TSource&gt; source, params Func&lt;TSource, double?&gt;[] selectors) { if (selectors.Length == 0) { return null; } else { double?[] result = new double?[selectors.Length]; for (int i = 0; i &lt; selectors.Length; i++) { result[i] = 0; } foreach (var item in source) { for (int i = 0; i &lt; selectors.Length; i++) { double? value = selectors[i](item); if (value != null) { result[i] += value; } } } return result; } } } </code></pre> <p>Here's the way you have to do the summation:</p> <pre><code>double[] result = m.Items.SumMany(p =&gt; p.Total, q =&gt; q.Done); </code></pre> <p>Here's a general example:</p> <pre><code>struct MyStruct { public double x; public double y; } MyStruct[] ms = new MyStruct[2]; ms[0] = new MyStruct() { x = 3, y = 5 }; ms[1] = new MyStruct() { x = 4, y = 6 }; // sum both x and y members in one iteration without duplicating the array "ms" by GROUPing it double[] result = ms.SumMany(a =&gt; a.x, b =&gt; b.y); </code></pre> <p>as you can see </p> <pre><code>result[0] = 7 result[1] = 11 </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. This table or related slice is empty.
    1. This table or related slice is empty.
    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