Vb.net 使用两个表将linq转换为实体
编辑:我正在检索一些订单数据,并试图使用第一个表中的productID从另一个表中获取产品描述。下面的代码工作正常,但我需要使用productid查询products表以获取字符串productdescriptionVb.net 使用两个表将linq转换为实体,vb.net,linq,Vb.net,Linq,编辑:我正在检索一些订单数据,并试图使用第一个表中的productID从另一个表中获取产品描述。下面的代码工作正常,但我需要使用productid查询products表以获取字符串productdescription Dim result = From c In ctx.orders Where c.CustomerId = 13 Select New With {c.OrderD
Dim result = From c In ctx.orders
Where c.CustomerId = 13
Select New With
{c.OrderDate,
c.PurchaseOrderNumber,
c.ProductId,
c.ProductPrice,
c.ProductQty}
我尝试了下面的建议,得到了一个名为productid的字段或属性不存在错误
使用这种方法
Dim result = From c In ctx.orders
Where c.CustomerId = 13
Join prod In ctx.products on c.ProductId Equals prod.Id
Select New With
{c.OrderDate,
c.PurchaseOrderNumber,
prod.Description,
c.ProductPrice,
c.ProductQty}
为什么不使用和
我制作了一个示例类来测试连接,这非常有效。您确定它加载了products表并具有该属性吗
Module Module1
Sub Main()
Dim ctx As List(Of X) = New List(Of X)
Dim x1 As X = New X()
x1.One = 1
x1.Two = 2
Dim x2 As X = New X()
x2.One = 10
x2.Two = 20
ctx.Add(x1)
ctx.Add(x2)
Dim ctx2 As List(Of Y) = New List(Of Y)
Dim y1 As Y = New Y()
y1.Three = 1
y1.Four = 2
Dim y2 As Y = New Y()
y2.Three = 10
y2.Four = 20
ctx2.Add(y1)
ctx2.Add(y2)
Dim result = From c In ctx
Join prod In ctx2 On c.One Equals prod.Three
Where c.One = 1
Select New With
{c.One,
prod.Four,
c.Two}
For Each a In result
Console.Beep()
Next
Console.Read()
End Sub
Class X
Public Property One As Integer
Public Property Two As Integer
End Class
Class Y
Public Property Three As Integer
Public Property Four As Integer
End Class
End Module
为什么不使用这个:
Dim结果=来自ctx订单中的c
其中c=>c.CustomerId=13
.选择manyc=>c.产品;
然后您将获得订单中的所有产品。VS不喜欢进入产品描述部分。我在某种程度上将查询改为prodDesc,并将prodDesc.Description改为prod.Description,结果表明不存在字段productId,这显然是错误的true@dinotom尝试更新的代码。对不起,我在做一些C类的东西,看起来有一些细微的差别。这与我在你最初的建议中所做的相似,我仍然在所选数据上找到名为productId的相同错误字段或属性,但它确实填充了网格的第一列,它似乎在prod上呕吐。描述现在是否加载了产品,我之所以这样问,是因为当我正常地看到ctx时,上下文有时是懒惰的loaded@dinotom我发布了一个示例连接,使用一些一次性类来说明连接。您能否验证ctx.products集合是否有数据,以及该数据是否包含产品Id?
Module Module1
Sub Main()
Dim ctx As List(Of X) = New List(Of X)
Dim x1 As X = New X()
x1.One = 1
x1.Two = 2
Dim x2 As X = New X()
x2.One = 10
x2.Two = 20
ctx.Add(x1)
ctx.Add(x2)
Dim ctx2 As List(Of Y) = New List(Of Y)
Dim y1 As Y = New Y()
y1.Three = 1
y1.Four = 2
Dim y2 As Y = New Y()
y2.Three = 10
y2.Four = 20
ctx2.Add(y1)
ctx2.Add(y2)
Dim result = From c In ctx
Join prod In ctx2 On c.One Equals prod.Three
Where c.One = 1
Select New With
{c.One,
prod.Four,
c.Two}
For Each a In result
Console.Beep()
Next
Console.Read()
End Sub
Class X
Public Property One As Integer
Public Property Two As Integer
End Class
Class Y
Public Property Three As Integer
Public Property Four As Integer
End Class
End Module