1
我是新的LINQ,我需要幫助轉換這個SQL代碼:要LINQ轉換加入和集團通過從SQL到LINQ
SELECT UserId_FK,COUNT(OrderId),SUM(Quantity)
FROM Orders o
JOIN OrderDetails od
ON(o.OrderId = od.OrderId_FK)
GROUP BY UserId_FK;
我是新的LINQ,我需要幫助轉換這個SQL代碼:要LINQ轉換加入和集團通過從SQL到LINQ
SELECT UserId_FK,COUNT(OrderId),SUM(Quantity)
FROM Orders o
JOIN OrderDetails od
ON(o.OrderId = od.OrderId_FK)
GROUP BY UserId_FK;
沒有看到你的架構文件,我將採取猜測這裏:
var userDetail =
context.Orders
.GroupBy(i => i.UserId_Fk)
.Select(i => new {
UserId_Fk = i.Key,
OrderCount = i.Count(),
ProductQuantity = i.Sum(j => context.OrderDetails.Where(k=> k.OrderId_Fk == j.OrderId).Sum(k=> k.Quantity))
});
啊,我剛剛發佈你打我。 –
Thx傢伙即將試用 – Eliza
看看[LINQPad](http://linqpad.net)它將SQL轉換爲LINQ(等等)。 – ChrisF
這是linq問題的好資源。 http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b –