2012-05-24 31 views
0

我想在網格中顯示訂單信息附近的OrderDetails Count,但在選擇單元中,我只能選擇Key和Count。選擇訂單信息的方式是什麼?使用Linq計數

var Q = from Data in Context.Orders 
     join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID 
     group Data by Data.OrderID into grouped 
         select new 
           { 
            grouped=g.Key, 
            Count = grouped.Count() 
           }; 
+0

選擇_what_ 「訂單信息」? –

+0

我想選擇所有的訂單信息,然後顯示在gridview –

+0

順便說一句,在最後一列orderDetail的計數順便說一句,你是不是指'grouped = grouped.Key'? –

回答

2

您可以通過整個訂單的實體組,彷彿是對象的內存集合

var Q = from Data in Context.Orders 
     join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID 
     group Data by Data into grouped 
         select new 
           { 
            OrderId = grouped.Key.OrderId, 
            OrderDate = grouped.Key.OrderDate 
            Shipping = grouped.Key.Shipping 
            . 
            . 
            . 
            Count = grouped.Count() 
           }; 

編輯 Linqpad程序類似查詢上

void Main() 
{ 
    var orders = new List<Order>{ 
     new Order{OrderId = 1, DeliverIn = 5}, 
     new Order{OrderId = 2, DeliverIn = 6}, 
     new Order{OrderId = 3, DeliverIn = 5}, 
    }; 

    var lines = new List<OrderLine>{ 
     new OrderLine{LineId = 1, OrderId = 1, ProductId = 1}, 
     new OrderLine{LineId = 2, OrderId = 1, ProductId = 2}, 
     new OrderLine{LineId = 3, OrderId = 1, ProductId = 3}, 

     new OrderLine{LineId = 4, OrderId = 2, ProductId = 1}, 
     new OrderLine{LineId = 5, OrderId = 2, ProductId = 3}, 
     new OrderLine{LineId = 6, OrderId = 2, ProductId = 4}, 
    }; 

    var query = from o in orders join l in lines on 
      o.OrderId equals l.OrderId 
      group o by o into grouped 
      select new 
      { 
       Count = grouped.Count(), 
       grouped.Key.OrderId, 
       grouped.Key.DeliverIn 
      }; 
      Console.WriteLine(query); 
} 

// Define other methods and classes here 
public class Order 
{ 
    public int OrderId{get;set;} 
    public int DeliverIn{get;set;} 

} 

public class OrderLine 
{ 
    public int LineId{get;set;} 
    public int OrderId{get;set;} 
    public int ProductId{get;set;} 
} 

,如果你沒有linq墊s意味着去從their site抓住它。這簡直太棒了。

+0

OrderId = grouped.Key.OrderId, 不工作 –

+0

它應該工作。我在內存對象集合中測試了它。讓我更新答案以顯示該代碼。 –

+0

感謝您的回答是正確的,它的工作方式。有沒有一種方法可以從OrderLine中選擇ProductId? –

1

查看IGrouping關於MSDN的文檔。

public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>, 
IEnumerable 

請注意IEnumerable。 Count只是IEnumerable的擴展方法。您可以輕鬆地從分組中選擇或循環選擇。

例如:

var Q = from Data in Context.Orders 
    join D2 in Context.OrderDetails on Data.OrderID equals D2.OrderID 
    group Data by Data.OrderID into grouped 
        select new 
          { 
           grouped=g.Key, 
           Count = grouped.Count(), 
           Orders = grouped.ToArray() 
//you can also just return grouped itself to support lazy queries 
          }; 
0

只是將它們壓扁成數組或列表,然後得到它的數量。

select new 
{ 
    Key = g.Key, 
    Orders = grouped.ToArray() 
}; 

然後得到數:

int count = result.Orders.Count; // Property of an array.