Looking for a JavaEE Architect or Potential Tech Co-Founder?
Please don't hesitate to contact me.

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();
How to execute a group by and sum query in entity framework How to execute a group by and sum query in entity framework Reviewed by Edward Legaspi on Sunday, March 04, 2012 Rating: 5

No comments:

Powered by Blogger.