Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>LINQ does contain a way to group your data. Start by taking a look at this statement:</p> <pre><code>// group by Year-Month var rows = from s in saleList orderby s.MonthYear group s by new { Year = s.MonthYear.Year, Month = s.MonthYear.Month }; </code></pre> <p>The above statement will take your data and group it by Year-Month so that it will create a main key for each Year-Month combination and create a group of all corresponding <code>SaleMonth</code> items into that group.</p> <p>When you grasp that, the next step is to use those groups to calculate whatever you want to calculate within each group. So, if you were just looking to total all the <code>FullMonths</code> and <code>DaysMonths</code> for each Year-Month, you could do this:</p> <pre><code>var rowsTotals = from s in saleList orderby s.MonthYear group s by new { Year = s.MonthYear.Year, Month = s.MonthYear.Month } into grp select new { YearMonth = grp.Key.Year + " " + grp.Key.Month, FullMonthTotal = grp.Sum (x =&gt; x.FullMonth), DaysMonthTotal = grp.Sum (x =&gt; x.DaysMonth) }; </code></pre> <p><strong>Edit:</strong></p> <p>After looking again at what you're doing, I think it would be more efficient to do this:</p> <pre><code>// populate our class with the time period we are interested in var startDate = saleList.Min (x =&gt; x.ActivationDate); var endDate = saleList.Max (x =&gt; x.EndDate); List&lt;SaleMonth&gt; salesReport = new List&lt;SaleMonth&gt;(); for(var i = new DateTime(startDate.Year, startDate.Month, 1); i &lt;= new DateTime(endDate.Year, endDate.Month, 1); i = i.AddMonths(1)) { salesReport.Add(new SaleMonth { MonthYear = i }); } // loop through each Month-Year foreach(var sr in salesReport) { // get all the sales that happen in this month var lastDayThisMonth = sr.MonthYear.AddMonths(1).AddDays(-1); var sales = from s in saleList where lastDayThisMonth &gt;= s.ActivationDate, where sr.MonthYear &lt;= s.EndDate select s; // calculate the number of days the sale spans for just this month var nextMonth = sr.MonthYear.AddMonths(1); var firstOfNextMonth = sr.MonthYear.AddMonths(1).AddDays(-1).Day; sr.DaysMonth = sales.Sum (x =&gt; (x.EndDate &lt; nextMonth ? x.EndDate.Day : firstOfNextMonth) - (sr.MonthYear &gt; x.ActivationDate ? sr.MonthYear.Day : x.ActivationDate.Day)); // how many sales occur over the entire month sr.FullMonth = sales.Where (x =&gt; x.ActivationDate &lt;= sr.MonthYear &amp;&amp; nextMonth &lt; x.EndDate).Count (); } </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