entity framework - How to use LINQ Group By with Count -
i want convert next sql query entity framework + linq query. there 3 tables brands, products , productreviews. products table has brandid fk , productreviews has productid fk.
select top 5 b.id, b.shortname, count(r.id) totalreviews productsreviews r inner bring together products p on r.productid = p.id inner bring together brands b on p.brandid = b.id grouping b.id, b.shortname order totalreviews desc
basically, want display top 5 brands based on reviews posted products of brands. want output below:
id shortname totalreviews ----------------------------------------- 76 adidas 61 120 yamaha 29 109 tommy hilfiger 26 61 mothercare 25 31 haier 22
pseudocode
var results = ( r in productsreviews bring together p in products on r.productid equals p.id bring together b in brands on p.brandid equals b.id grouping c new { b.id, b.shortname } grp select new { id = grp.key.id, shortname = grp.key.shortname, totalreviews = grp.count()} ) .orderby(x=>x.totalreviews).take(5);
linq entity-framework group-by
No comments:
Post a Comment