2
我搜索了以前的问题,似乎找不到我在找什么,所以请原谅n00b LINQ的问题......我正在努力变得熟悉与LINQ,让我的头在语法和用法。LINQ:加入三个表的“组加入”功能
在我的训练,我总结了以下查询:
Dim r2 = From cust In cData.Customers _
Group Join ord In cData.Orders
On cust.CustomerID Equals ord.CustomerID
Into custorders = Group
这个查询我的理解,这让我对每个卡斯特对象我卡斯特对象,然后IEnumerable的custorders集合。
我似乎无法弄清楚的是,如何实现相同格式的结果,但添加第三个表“产品”使用组加入,所以我可以返回“产品”字段,如“ProductName”和“ ProductPrice”。
我有以下三个来源:
Public Customers As New List(Of Customer)
Public Products As New List(Of Product)
Public Orders As New List(Of Order)
相关领域是:
Orders.OrderID
Orders.OrderDate
Orders.CustomerID
Orders.ProductID
Customers.CustomerID
Customers.FirstName
Customers.LastName
Products.ProductID
Products.ProductName
Products.ProductPrice
我想我的第一个问题是,我应该使用组加入在这一点?如果是这样,我将如何撰写查询?如果没有,是否有一种简单,直接的方式来使用LINQ实现相同的结果?
任何帮助,将不胜感激!
UPDATE:
我想出了下面的查询,似乎解决我的问题:
Dim r4 = From cust In cData.Customers, ord In cData.Orders, _
prod In cData.Products _
Where ord.CustomerID = cust.CustomerID And _
ord.ProductID = prod.ProductID _
Group ord, prod By cust Into custorders = Group
所以我想我的问题在这一点上会,这是最好的方式来完成我的目标?或者使用另一种方法更有效率?