.net SQL Group By+;的LINQ/Lambda查询帮助;最大值?

.net SQL Group By+;的LINQ/Lambda查询帮助;最大值?,.net,linq,entity-framework,.net,Linq,Entity Framework,我想知道是否有人可以帮助我使用基于以下SQL查询的lambda或LINQ查询(最好是lambda)。我已经试过了,但没有成功:( SQL将返回如下结果: MaxProductID | RegionID | ProductCount 123 | 16862 | 3 我在“max result”列后面,其中max()将是客户凭证记录以及找到的productID最多的关系 谢谢各位。假设ctx是您的实体模型,请使用以下方法 var result = from cv in ctx.CustomerV

我想知道是否有人可以帮助我使用基于以下SQL查询的lambda或LINQ查询(最好是lambda)。我已经试过了,但没有成功:(

SQL将返回如下结果:

MaxProductID | RegionID | ProductCount
123 | 16862 | 3
我在“max result”列后面,其中
max()
将是客户凭证记录以及找到的productID最多的关系


谢谢各位。

假设ctx是您的实体模型,请使用以下方法

 var result = from cv in ctx.CustomerVoucher
                         join p in ctx.Products on p.id equals cv.ProductID && p.status == 3
                         join ap in ctx.APs on ap.id equals prop.apid
                         where cv.status == 1
                         group cv by cv.ProductID into g
                         select new { MaxProductID = g.Max(cv => cv.ProductID), RegionID = g.max(ap => ap.RegionAsLocationID), ProductCount = g.Count(cv => cv.ProductID) };
非常感谢:)经过一些调整和FirstOrDefault()使它工作得非常完美!
 var result = from cv in ctx.CustomerVoucher
                         join p in ctx.Products on p.id equals cv.ProductID && p.status == 3
                         join ap in ctx.APs on ap.id equals prop.apid
                         where cv.status == 1
                         group cv by cv.ProductID into g
                         select new { MaxProductID = g.Max(cv => cv.ProductID), RegionID = g.max(ap => ap.RegionAsLocationID), ProductCount = g.Count(cv => cv.ProductID) };