0

我需要使用 Linq to Entities 获取前 10 名记录。

我需要将返回的结果绑定到GridView如下“

Product Name  |   Product Description  |   Number of Items Sold

Item 1        |   Item 1 Description   |           24

如上所述,物品需要按顺序存放,从售出的最多物品开始。

我已经尝试了几个例子,比如thisthis,并按照这里的例子

以下是我迄今为止尝试过的:

public IQueryable GetTopTenProducts(DateTime startDate, DateTime endDate)
{
    return
        (from p in this.Entities.Product
         join pro in this.Entities.ProductOrder on p.ID equals pro.ProductID
         join o in this.Entities.Order on pro.OrderID equals o.ID
         where o.DateOfOrder >= startDate && o.DateOfOrder <= endDate
         select new
         {
             Product = p,
             Quantity = pro.Qty,
             ProductName = p.Name,
             ProductDescription = p.Description

         } into productQty
         group productQty by productQty.Product into pg
         let totalQuantity = pg.Sum(prod => prod.Quantity)
         orderby totalQuantity descending
         select pg.Key).Take(10);
}

这将返回整个产品表,但我只需要检索上面粘贴的详细信息。

有任何想法吗?

4

2 回答 2

5

我认为你必须改变你最后的选择select pg.Key来选择你想要的

例如:select new { pg.Key.ProductName, pg.Key.ProductDescription, totalQuantity }

于 2014-01-03T13:52:19.703 回答
1

您将获得整个产品表,因为您选择pg.Keywhere pgis an IGroupingwithProduct作为键,您的匿名类型作为IElement.

因此,您可以重构最终选择以执行以下操作:

//rest of Linq query...
select new 
{ 
    pg.Key.Name, 
    pg.Key.Description,
    totalQuantity
})Take(10);
于 2014-01-03T13:58:19.473 回答