【问题标题】:linq to entity using two tableslinq 到使用两个表的实体
【发布时间】:2012-11-16 00:06:54
【问题描述】:

已编辑:我正在检索一些订单数据,并尝试使用第一个表中的 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}

【问题讨论】:

    标签: vb.net linq


    【解决方案1】:

    为什么不使用join operator C# versionjoin operator VB version

    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}
    

    我制作了一个示例类来测试连接,并且效果很好。你确定它加载了 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
    

    【讨论】:

    • VS 不喜欢“into prodDesc”部分。我将查询更改为将“转换为 prodDesc”并将 prodDesc.Description 更改为 prod.Description 并错误地说(“没有字段 productId 存在),这显然不是真的
    • @dinotom 试试更新的代码。抱歉,我在做一些 C# 的东西,看起来有一些细微的差别。
    • 这与我在您最初的建议中所做的类似,我仍然收到相同的错误“在所选数据上找不到名为 productId 的字段或属性,但它确实填充了网格的第一列(它似乎在 prod.description 上呕吐)
    • 现在已经加载了产品,我只是问,因为当我正常看到 ctx 时,有时会延迟加载上下文
    • @dinotom 我发布了一个示例连接,使用一些一次性类来说明连接。您能否验证 ctx.products 集合有数据并且该数据包含产品 ID?
    【解决方案2】:

    为什么不使用这个: 暗淡的结果 = 来自 c 在 ctx.orders .Where(c => c.CustomerId = 13) .SelectMany(c => c.products); 然后您将获得订单中的所有产品。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多