2011-12-24 19 views
2

是否有可能将这个sql代码转换为linq?从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

回答

1
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在组中保持每一行。