1

已编辑:我正在检索一些订单数据,并尝试使用第一个表中的 productID 从另一个表中获取产品描述。下面的代码工作正常,但我需要使用 productid 查询产品表以获取字符串、产品描述

 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}
4

2 回答 2

0

为什么不用join算子C#版join算子VB版

Dim result = From c In ctx.orders
             Join prod In ctx.products On c.ProductId Equals prod.Id
             Where c.CustomerId = custid
             Select New With
                    {c.OrderDate,
                      prod.Description
                     c.ProductPrice,
                     c.ProductQty}

我制作了一个示例类来测试连接,并且效果很好。你确定它加载了产品表并且它具有属性吗?

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
于 2012-11-16T00:11:16.620 回答
0

为什么不使用这个: Dim result = From c In ctx.orders .Where(c => c.CustomerId = 13) .SelectMany(c => c.products); 然后您将获得订单中的所有产品。

于 2012-11-16T01:32:16.037 回答