0

This is not same as Is it possible to Pivot data using LINQ? as I need 2 years monthly data to compare. I am writing LINQ query to get the sales report but I am not sure how to achieve the expected outcome with LINQ query. Can you please guide me?

Expected: enter image description here Currently I am getting this: enter image description here

Current linq query:

var sales = from s in legacyContext.For<Sales>().Query
    join p in legacyContext.For<Product>().Query
    on s.ProductId equals p.ProductId                        

    select new
    {
        p.ProductName,
        s.TotalSales,
        CreatedDateMonth = s.CreationUtcTime.Month.ToString(),
        CreatedDateYear = s.CreationUtcTime.Year.ToString(),
    };

var monthlySales = (from s in sales
    group s by new { s.CreatedDateYear, s.CreatedDateMonth, s.ProductName } into cgrp
    select new Report
    {
        ProductName = cgrp.Key.ProductName,
        CreatedDateYear = cgrp.Key.CreatedDateYear,
        CreatedDateMonth = cgrp.Key.CreatedDateMonth,
        CreatedDateMonth = cgrp.Key.CreatedDateMonth,
        TotalSales = cgrp.Sum(x => x.TotalSales)
    });
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • It's called "pivot" (think of rotating a couch through the stairwell). See duplicate. – CodeCaster Nov 25 '21 at 11:56
  • What does "I need 2 years monthly data to compare" mean? And why isn't that still a pivot? Your question and the image make it look very much like it is a pivot. – Enigmativity Nov 25 '21 at 23:57

0 Answers0