How to Execute a Group by and Sum Query in Entity Framework

The following code is the native sql with the converted code in entity framework. It queries the sum of quantity group by branch, model, year and week no.
//native sql
select weekyear, weekno, branchid, modelid, sum(quantity) from selloutmobiles
where weekyear=2012 and weekno=1
group by weekyear, weekno, branchid, modelid

//entity framework
var count = (from aa in Context.Products
                         where aa.BranchId == branchId
                         && aa.ModelId == skuId
                         && aa.WeekYear == year
                         && aa.WeekNo == week
                         group aa by new { aa.BranchId, aa.ModelId, aa.WeekYear, aa.WeekNo }
                             into gaa
                             select gaa.Sum(aa => aa.Quantity)).FirstOrDefault();

0 Comments

Post a Comment

Post a Comment (0)

Previous Post Next Post
NERV Open Source

Building production Spring Boot systems?

Explore NERV — open-source Java libraries for audit trails, persistence, exception handling, and reliable event-driven architecture.

Explore NERV on GitHub →