Reputation: 966
I'm kind of stuck with a problem that I'm having. Among others, I have this tables in my DB:
Product (int productId, ...otherProductInfo)
Customer (int customerId, ...otherCustomerInfo)
SoldToData ( int productId, int customerId)
I want to get top ten selling products using Entity Framework in MVC2. How can I do that?
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Following thekip's and Pr0fess0rX's advices, this is what I have done so far, and it seems to be working:
using (Entities db = new Entities())
{
var groupedProducts = (from p in db.Products
join s in db.SoldToData
on p.productId equals s.productId
group p by p.id
into ProductGroup
orderby ProductGroup.Count() descending
select ProductGroup).Take(10).ToList();
List<Products> products = new List<Products>();
products.AddRange(groupedProducts.Select(gp => gp.First()));
}
Is this the proper way?
Upvotes: 1
Views: 7568
Reputation: 6030
Upvotes: 1
Reputation: 3768
If you have an IQueryable to query the datasource you could use orderby etc for ordering followed by Take(10)?
Upvotes: 4