In Entity Framework, get the top N rows in the table.

asp.net-mvc-2 c# entity-framework

Question

I feel a little trapped by the issue I'm facing. In my database, I also have the following tables:

Product (int productId, ...otherProductInfo)
Customer (int customerId, ...otherCustomerInfo)
SoldToData ( int productId, int customerId)

I want to use Entity Framework in MVC2 to get the top 10 selling goods. How can I go about doing that?

/ I have so far followed thekip's and Pr0fess0rX's instructions, and it seems to be effective:

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 right procedure?

1
1
6/6/2011 12:16:45 PM

Accepted Answer

If you have an IQueryable, you could use orderby etc. for ordering and Take(10) after that?

4
6/6/2011 11:00:30 AM

Popular Answer

  1. associer clients and goods
  2. Get the number of items sold to each group of customers.
  3. Descending in order of count
  4. Take the top 10 times.
  5. Identify the result goods by name (top 10)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow