locked
How to group by month and year RRS feed

  • Question

  • User-1104215994 posted

    Hello,

    I am grouping by a table as follows. In this <g class="gr_ gr_65 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="65" data-gr-id="65">table</g> there is a <g class="gr_ gr_88 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="88" data-gr-id="88">dattime</g> field. (<g class="gr_ gr_163 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="163" data-gr-id="163">requestdate</g> = 2019-09-08 21:38:36.517) I wonder if I can group by month and year?

    //Group By games
                var games = await _context.GameBanks.GroupBy(g => new {g.ProductCode, g.UnitPrice, g.ProductDescription})
                    .Select(gcs => new GameBanks
                    {
                        ProductCode = gcs.Key.ProductCode,
                        ProductDescription = gcs.Key.ProductDescription,
                        UnitPrice = gcs.Key.UnitPrice,
                        Quantity = gcs.Sum(g => g.Quantity)
                    }).ToListAsync();

    Monday, September 9, 2019 12:35 PM

Answers

  • User-1104215994 posted

    this worked.

    var games = await _context.GameBanks.GroupBy(g => new {g.ProductCode, g.UnitPrice, g.ProductDescription, g.RequestDateTime.Value.Year ,g.RequestDateTime.Value.Month })
                    .Select(gcs => new GameBanks
                    {
                        ProductCode = gcs.Key.ProductCode,
                        ProductDescription = gcs.Key.ProductDescription,
                        UnitPrice = gcs.Key.UnitPrice,
                        Quantity = gcs.Sum(g => g.Quantity)
    
                    }).ToListAsync();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 11, 2019 2:10 PM

All replies

  • User-17257777 posted

    Hi cenk1536,

    You can use requestdate.Tostring(“y”) to get the year and month, then Group by it. I have made a test with the following codes:

    List<GameBanks> gbs = new List<GameBanks>
    {
    new GameBanks{ProductCode = "1", Quantity = 11, requestdate = DateTime.Now,
    ProductDescription = "xxxx", UnitPrice = "100"},
    new GameBanks{ProductCode = "1", Quantity = 22, requestdate = DateTime.Now.AddYears(-1),
    ProductDescription = "xxxx", UnitPrice = "100"},
    new GameBanks{ProductCode = "1", Quantity = 33, requestdate = DateTime.Now.AddYears(-1),
    ProductDescription = "xxxx", UnitPrice = "100"},
    new GameBanks{ProductCode = "2", Quantity = 44, requestdate = DateTime.Now.AddMonths(-1),
    ProductDescription = "xxxx", UnitPrice = "80"},
    new GameBanks{ProductCode = "2", Quantity = 55, requestdate = DateTime.Now.AddMonths(-1),
    ProductDescription = "xxxx", UnitPrice = "80"},
    new GameBanks{ProductCode = "2", Quantity = 66, requestdate = DateTime.Now.AddYears(-1).AddMonths(-1),
    ProductDescription = "xxxx", UnitPrice = "80"}
    };
    var games = gbs.GroupBy(g => new { g.ProductCode, g.UnitPrice, g.ProductDescription, date = g.requestdate.ToString("y") })
    .Select(gcs => new GameBanks
    {
    ProductCode = gcs.Key.ProductCode,
    ProductDescription = gcs.Key.ProductDescription,
    UnitPrice = gcs.Key.UnitPrice,
    Quantity = gcs.Sum(g => g.Quantity)
    }).ToList();

    Test Result:

    Best Regards,

    Jiadong Meng

    Tuesday, September 10, 2019 7:08 AM
  • User-1104215994 posted

    No overload method ToString takes 1 arguments.

    Wednesday, September 11, 2019 1:50 PM
  • User-1104215994 posted

    this worked.

    var games = await _context.GameBanks.GroupBy(g => new {g.ProductCode, g.UnitPrice, g.ProductDescription, g.RequestDateTime.Value.Year ,g.RequestDateTime.Value.Month })
                    .Select(gcs => new GameBanks
                    {
                        ProductCode = gcs.Key.ProductCode,
                        ProductDescription = gcs.Key.ProductDescription,
                        UnitPrice = gcs.Key.UnitPrice,
                        Quantity = gcs.Sum(g => g.Quantity)
    
                    }).ToListAsync();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 11, 2019 2:10 PM