【问题标题】:linq joining tables errorlinq 连接表错误
【发布时间】:2013-12-07 07:53:39
【问题描述】:

加入表格时出现错误,将结果作为列表返回。这是我的代码。请帮忙。谢谢

Public Function GetMerchantList() As List(Of Merchant)
        Dim Db As New TTMSEntities

        Dim Data = From p In Db.TT_MERCHANT Join r In Db.TT_BRANCH_SETTING On _
                   p.MERCHANT_BRANCH_INTERNAL_NUM Equals r.INTERNAL_NUM
                   Select New With {p.MERCHANT_ID, p.DESCRIPTION, r.INTERNAL_NUM, r.BRANCH_DESC}

        If Data IsNot Nothing Then
            Return ConvertMerchant(Data)
        Else
            Return Nothing
        End If
    End Function

错误 无法将“System.Data.Entity.Infrastructure.DbQuery1[VB$AnonymousType_04[System.String,System.String,System.Decimal,System.String]]”类型的对象转换为“System.Collections.Generic.List”类型1[TTMS.App.WebSites.Data.Merchant]

【问题讨论】:

  • 你能提供ConvertMerchant代码吗?

标签: vb.net linq


【解决方案1】:

您的 Data 变量具有匿名类型的 IQueryable 类型,因此您需要指定您想要的类型并调用 ToList 类似的东西

Public Function GetMerchantList() As List(Of Merchant)
    Dim Db As New TTMSEntities

    Dim Data = (From p In Db.TT_MERCHANT Join r In Db.TT_BRANCH_SETTING On _
               p.MERCHANT_BRANCH_INTERNAL_NUM Equals r.INTERNAL_NUM
               Select New Merchant With {p.MERCHANT_ID, p.DESCRIPTION, r.INTERNAL_NUM, r.BRANCH_DESC}).ToList()

    If Data IsNot Nothing Then
        Return ConvertMerchant(Data)
    Else
        Return Nothing
    End If
End Function

还有Data 总是不为空(或者对于 VB 为空)它可以为空

【讨论】:

  • 正确,但Data 绝不是Nothing。它可以是一个空列表。
  • @GertArnold 在回答的最后我说了同样的话 :-)
猜你喜欢
  • 1970-01-01
  • 2011-09-09
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
相关资源
最近更新 更多