2009-07-08 87 views
1

內joning我想翻譯成Linq的這一點,並不能弄明白:多個分組,Linq中

SELECT 
    CustomerOrder.ShipState, MONTH(OrderFulfillment.OrderDate) AS Mnth, 
    YEAR(OrderFulfillment.OrderDate) AS Yer, 
    SUM(OrderFulfillment.Tax) AS TotalTax 
FROM 
    OrderFulfillment INNER JOIN 
     CustomerOrder ONOrderFulfillment.OrderID =CustomerOrder.OrderID 
WHERE 
    (OrderFulfillment.Tax > 0) 
GROUP BY 
    CustomerOrder.ShipState, MONTH(OrderFulfillment.OrderDate), 
    YEAR(OrderFulfillment.OrderDate) 
ORDER BY 
    YEAR(OrderFulfillment.OrderDate) DESC, CustomerOrder.ShipState, 
    MONTH(OrderFulfillment.OrderDate) DESC 

我有Linqpad並通過了一堆的例子已經走了,但可能不知道這一點。

回答

0

我想你想要做這樣的事情:

from c in CustomerOrder 
    join o in OrderFulfillment on c.OrderId equals o.OrderId 
where 
    o.Tax > 0 
group o by 
    new { c.ShipState, Mnth = of.OrderDate.Month, Yer = of.OrderDate.Year } 
     into g 
orderby 
    g.Key.Yer descending, g.ShipState, g.Key.Mnth descending 
select 
    new { g.Key.ShipState, g.Key.Mnth, g.Key.Yer, 
    TotalTax = g.Sum(i => i.Tax) }; 

我沒試過編譯它,但我認爲這是沿着你想要的東西。

這個想法是,你首先執行你的連接來連接客戶和訂單。然後應用您的過濾條件。

此時,您想要獲得具有特定組的所有訂單,因此應用了組操作員。

最後,對結果進行排序,然後從每個組的關鍵字中選擇所有信息,並對每個組中的稅進行總結。

0

首先,很高興知道你無法弄清楚什麼。如果你完全迷失了,並且不知道從哪裏開始,那麼你需要谷歌來進行linq加入和分組。

這裏的東西我最近可能(可能)你指出正確的方向:

// This groups items by category and lists all category ids and names 
from ct in Categories 
    join cst in Category_Subtypes 
    on ct.Category_Id equals cst.Category_Id 
    join st in Subtypes 
    on cst.Subtype_Id equals st.Subtype_Id 
where 
    st.Type_Id == new Guid(id) 
group ct by new { ct.Category_Id, ct.Display_Text } into ctg 
select new 
{ 
    Id = ctg.Key.Category_Id, 
    Name = ctg.Key.Display_Text 
} 
+0

這是一個很大的幫助 - 謝謝! – Slee 2009-07-08 15:29:15