SQL Group By 的 LINQ/Lambda 查询帮助最大限度?

发布于 2024-12-11 07:21:09 字数 616 浏览 0 评论 0原文

我想知道是否有人可以帮助我根据以下 SQL 查询进行 lambda 或 LINQ 查询(最好是 lambda)。我已经尝试并取得了一些进展,但没有成功:(

SELECT TOP 1 MAX(cv.ProductID) as MaxProductID, MAX(ap.RegionAsLocationID) RegionID,
   COUNT(cv.ProductID) as ProductCount 
FROM CustomerVouchers cv
INNER JOIN Products p on p.id = cv.ProductID and p.status = 3
INNER JOIN APs ap on ap.id = p.apid
WHERE cv.Status = 1
GROUP BY cv.ProductID
ORDER BY ProductCount DESC

SQL 返回这样的结果:

MaxProductID | RegionID | ProductCount
123 | 16862 | 3

我在“最大结果”列之后,其中 MAX() 将是客户凭证记录,它是找到最多productID 的关系,

谢谢大家。

I was wondering if anybody could help me with a lambda or LINQ query (preferrably lambda) based off the following SQL query. I have tried and got a little way through but no luck :(

SELECT TOP 1 MAX(cv.ProductID) as MaxProductID, MAX(ap.RegionAsLocationID) RegionID,
   COUNT(cv.ProductID) as ProductCount 
FROM CustomerVouchers cv
INNER JOIN Products p on p.id = cv.ProductID and p.status = 3
INNER JOIN APs ap on ap.id = p.apid
WHERE cv.Status = 1
GROUP BY cv.ProductID
ORDER BY ProductCount DESC

The SQL returns a result like this:

MaxProductID | RegionID | ProductCount
123 | 16862 | 3

I am after the "max result" columns where MAX() would be the customer vouchers record and it's relationships which has the most productID's found.

Thanks gang.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淤浪 2024-12-18 07:21:09

假设 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) };

Assuming ctx is your entity model, use the following.

 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) };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文