我有一个标准的商店架构与包含产品的InvoiceLine表。我希望以销售列表的形式列出前5名产品。最干净的方法是什么?这不可能是最好的方法:最受欢迎GroupBy与Linq实体
private List<Product> GetTopProducts(int count)
{
var topProducts = from invoiceLine in storeDB.InvoiceLines
group invoiceLine by invoiceLine.ProductId into grouping
orderby grouping.Count() descending
select new
{
Products = from Product in storeDB.Products
where grouping.Key == Product.ProductId
select Product
};
return topProducts.AsEnumerable().Cast<Product>().Take(count).ToList<Product>();
}
您的模型中有可见的FK,Jon?您没有,例如InvoiceLine.Product生成的属性? – 2010-02-26 00:45:28
我确实有一个InvoiceLine.Product。谢谢。那么返回它们最干净的方法是什么? – 2010-02-26 00:54:15