当有 group by 和 order by 时从 SQL 转换为 Linq

发布于 2024-12-22 21:20:14 字数 604 浏览 2 评论 0原文

是否可以将这段sql代码转换为linq?

SELECT top 3 t.tProductIDF, Count(t.tQty)as QtyBought, p.pName, v.vName
From Transactions t, Product p, Vendor v
where t.tProductIDF = p.pID and p.pdeleted = 0 and p.pVendorIDF = v.vID and v.vActive = 1
Group By t.tProductIDF, p.pName, v.vName
Order By QtyBought desc;

我目前在这里:

var topProds = (from t in this._entities.Transactions
                group t by t.tProductIDF into g
                orderby g.Count() descending
                select new {g.Key }).Take(3);

但由于我无法从选择部分访问 t,我不知道如何获取 pName 和 vName

is it possible to transform this sql code into linq?

SELECT top 3 t.tProductIDF, Count(t.tQty)as QtyBought, p.pName, v.vName
From Transactions t, Product p, Vendor v
where t.tProductIDF = p.pID and p.pdeleted = 0 and p.pVendorIDF = v.vID and v.vActive = 1
Group By t.tProductIDF, p.pName, v.vName
Order By QtyBought desc;

i am currently here:

var topProds = (from t in this._entities.Transactions
                group t by t.tProductIDF into g
                orderby g.Count() descending
                select new {g.Key }).Take(3);

but since i cannot access t from the select part, i do not know how i can get pName and vName

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

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

发布评论

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

评论(1

强者自强 2024-12-29 21:20:14
var topProds = (from t in this._entities.Transactions  
               group t by t.tProductIDF into g
               orderby g.Count() descending    
               select new { ProductIDF = g.Key , Qty= g.Sum(cc=>cc.tQty) , Vname = g.Select(cc=>cc.vName).FirstOrDefault() }).Take(3); 

你的 g 持有组中的每一行。

var topProds = (from t in this._entities.Transactions  
               group t by t.tProductIDF into g
               orderby g.Count() descending    
               select new { ProductIDF = g.Key , Qty= g.Sum(cc=>cc.tQty) , Vname = g.Select(cc=>cc.vName).FirstOrDefault() }).Take(3); 

Your g hold each row in group.

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