我對客戶及其訂單有以下代碼。我想列出所有的客戶ID和相應的訂單數量。我如何使用LINQ實現它?LINQ - 嵌套計數問題
注意:我是LINQ和var的新手。
protected void Page_Load(object sender, EventArgs e)
{
List<Order> orderListForCus1 = new List<Order>();
Order ord1 = new Order(1, new DateTime(2011, 9, 1));
Order ord2 = new Order(1, new DateTime(2011, 8, 1));
orderListForCus1.Add(ord1);
orderListForCus1.Add(ord2);
Customer cus1 = new Customer();
cus1.CustomerID = 1;
cus1.OrderList = orderListForCus1;
List<Customer> customerRecordsBook = new List<Customer>();
customerRecordsBook.Add(cus1);
var orderCounts = from c in customerRecordsBook
select new { c.CustomerID, OrderCount = c.OrderList.Count() };
}
你已經有了。你在問什麼? – SLaks
使用普通的'foreach'。 – SLaks