Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Entity Framework Grouping By Year And Month

$
0
0

i hardly use EF so not string in it.

code taken from https://www.mikesdotnetting.com/article/257/entity-framework-recipe-grouping-by-year-and-month

public ActionResult Index()
{
    var context = new EFRecipeContext();
    var model = context.Orders
        .GroupBy(o => new
        {
            Month = o.OrderDate.Month,
            Year = o.OrderDate.Year
        })
        .Select(g => new ArchiveEntry
        {
            Month = g.Key.Month,
            Year = g.Key.Year,
            Total = g.Count()
        })
        .OrderByDescending(a => a.Year)
        .ThenByDescending(a => a.Month)
        .ToList();

    return View(model);
}

stuck to understand this area specially

        .Select(g => new ArchiveEntry
        {
            Month = g.Key.Month,
            Year = g.Key.Year,
            Total = g.Count()
        })

why we need to select month like g.Key.Month ?

why can't we write g.Month ?

from where the key keyword comes in linq....it is never defined in code. help me to understand


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>