3

有一个 T-SQL 查询:

select [ProductNumber] ,max([ProductRevNumber])
from Products  
group by [ProductNumber]

尝试的 LINQ 查询:

ProductsDBContext.Products.GroupBy(x => x.ProductRevNumber)
                          .Select(group => ProductNumber, 
                                           ProductRevNumber = group.Max(x => x.ProductRevNumber));

lambda 查询不起作用。

4

2 回答 2

6

我认为这就是你所追求的:

var latestProducts = ProductsDBContext.Products
    .GroupBy(p => p.ProductNumber).Select(g => new 
    {
        ProductNumber = g.Key,
        MaxProductRevNumber = g.Max(p => p.ProductRevNumber))
    });
于 2012-07-13T16:28:36.977 回答
2
from p in db.Products
group p by p.ProductNumber into g
select new
{
    ProductNumber = g.Key,
    ProductRevNumber = g.Max(p => p.ProductRevNumber)
};
于 2012-07-13T16:35:00.257 回答