我的SQL数据库中有三个表
我希望实现这一点,我得到一个结果列表,其中每个对象都有一个customerid以及该客户的相关订单。
我在MSDN.com上看过这个例子,这几乎是我需要的,但只有2个表:
Dim customerList = From cust In customers
Group Join ord In orders On
cust.CustomerID Equals ord.CustomerID
Into CustomerOrders = Group,
OrderTotal = Sum(ord.Total)
Select cust.CompanyName, cust.CustomerID,
CustomerOrders, OrderTotal
我的代码现在只给我一个包含所有客户和订单的列表,但它们没有组合在一起。 就像它一样:
客户2 - 订单36
Dim results = From Customers In db.Customers
Join OrderCustomer In db.OrderCustomer on Customers.ID Equals OrderCustomer.CustomerId
Group Join Orders In db_alt.Orders On OrderCustomer.orderId Equals Orders.Id
Into CustomerOrders = Group,
OrderTotal = Sum(aufträge.Order_Value)
Select Customers.Firstname, Customers.Firstname, CustomerOrders, OrderTotal, Customers.ID
答案 0 :(得分:0)
如果所有外键都已正确设置,LINQ to SQL或EF应该会看到属性,你可以像这样查询:
var result = db.Customers.Include("Order") // EF
这应该带来所有customers
,并且每个人都会收集orders
或var result = db.Customers.Select(c=>new {Customer = c, Orders = c.Orders}