Divide one list into many by months - C #, Linq

I have the following Linq query:

List<MonthlySales> monthlySales = (from sale in Sales
                     group sale by new { sale.DateSold.Month, sale.Product.Shop } into ds
                     select new MonthlyTitleSales
                     {
                         Shop = ds.Select(it => it.Product.Shop).FirstOrDefault(),
                         Month = ds.Select(it => it.DateSold.Month).FirstOrDefault(),
                         Year = ds.Select(it => it.DateSold.Year).FirstOrDefault(),
                         USDNumberItemsSold = ds.Where(it => it.USDCut.HasValue).Where(it => it.USDCut != 0).Count(),
                         GBPNumberItemsSold = ds.Where(it => it.GBPCut.HasValue).Where(it => it.GBPCut != 0).Count(),
                         EURNumberItemsSold = ds.Where(it => it.EURCut.HasValue).Where(it => it.EURCut != 0).Count(),
                         USDRevenueTotal = PerformCurrencyConversion(ds.Sum(it => it.USDCut.HasValue ? it.USDCut.Value : 0), 0M, 0M, Currency, endDate),
                         GBPRevenueTotal = PerformCurrencyConversion(0M, ds.Sum(it => it.GBPCut.HasValue ? it.GBPCut.Value : 0), 0M, Currency, endDate),
                         EURRevenueTotal = PerformCurrencyConversion(0M, 0M, ds.Sum(it => it.EURCut.HasValue ? it.EURCut.Value : 0), Currency, endDate),
                     }).OrderBy(it => it.DateSold.Month).ToList();

This query gives me a list with the following elements in each element of the list: Shop, month, year, income Made

Ultimately, I need to calculate the median revenue for all stores for each month, so I need the data to be ready so that I can do it.

I thought that one way would be to break this list down into small lists by month, but I have doubts that this is the most effective solution.

Does anyone have any thoughts on this?

A monthly sales class for everyone who needs to see:

public class MonthlySales
    {
        public int Month { get; set; }
        public int Year { get; set; }

        public Shop Shop { get; set; }

        public int USDNumberItemsSold { get; set; }
        public int GBPNumberItemsSold { get; set; }
        public int EURNumberItemsSold { get; set; }
        public int TotalNumberItemsSold { get { return USDNumberItemsSold + GBPNumberItemsSold + EURNumberItemsSold; } }

        public decimal USDRevenueTotal { get; set; }
        public decimal GBPRevenueTotal { get; set; }
        public decimal EURRevenueTotal { get; set; }
        public decimal OverallEarnings { get { return USDRevenueTotal + GBPRevenueTotal + EURRevenueTotal; } }
    }
+3
source share
2 answers

GroupBy . , ( + , ), :

var grouped = monthlySales.GroupBy(ms => string.Format("{0}-{1}", ms.Month, ms.Year));

foreach(var group in grouped)
{
     Console.WriteLine("Values in month-year of {0}:", group.Key);
     foreach(var ms in group)
         Console.WriteLine("   {0}", ms.USDRevenueTotal);
}
+5

GroupBy . ToLookup ( , GroupBy ToLookup).

:

  • GroupBy .
  • ToLookup .

  • GroupBy, , , , . , GroupBy - .
  • ToLookup, - , , - .

  • GroupBy IEnumerable<IGrouping<TKey, TValue>>, .
  • ToLookup ILookup<TKey, TValue>, Dictionary<TKey, List<TValue>> , , IEnumerable<IGrouping<TKey, TValue>>.

- . , :

var grouped = monthlySales.GroupBy(ms => new {ms.Month, ms.Year}); 
+1

All Articles