linq加入表错误

时间:2013-12-07 07:53:40

标签: vb.net linq

加入表时出错,返回结果列表。这是我的代码。请帮忙。感谢

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.DbQuery 1[VB$AnonymousType_0 4 [System.String,System.String,System.Decimal,System.String]]'的对象,以键入'System.Collections.Generic。 List`1 [TTMS.App.WebSites.Data.Merchant]

1 个答案:

答案 0 :(得分: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来说都是空的)它可以是空的