2021-11-26

How can I achieve this in LINQ query [duplicate]

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)
    });


from Recent Questions - Stack Overflow https://ift.tt/3cPp2hc
https://ift.tt/3FPdHdk

No comments:

Post a Comment