【问题标题】:Unable to convert array to generic list无法将数组转换为通用列表
【发布时间】:2013-12-11 04:21:52
【问题描述】:

您好,我在将数组转换为通用列表时遇到了问题。它引发了如下错误。在课堂上,我已经以 ToList 的身份返回,但为什么我仍然会出现。我已经将服务引用返回类型更改为通用列表,默认情况下是 Array,但它获取 System.Collection.GenericList 无法转换为 System.Collection.Genericlist。请帮忙谢谢

Public Function GetMerchantList() As List(Of Merchant) Implements IMerchant.GetMerchantList
    Dim ws As New aMerchantService.MerchantServiceClient

    Dim General As New General
    Dim kWSUrl As String = ""

    Dim endpointAddress = ws.Endpoint.Address

    Dim newEndpointAddress As New EndpointAddressBuilder(endpointAddress)
    kWSUrl = General.ConvertWsURL("App")

    newEndpointAddress.Uri = New Uri(kWSUrl & "MerchantService.svc")
    ws = New aMerchantService.MerchantServiceClient("BasicHttpBinding_IMerchantService", newEndpointAddress.ToEndpointAddress())
    Dim Data = ws.GetMerchantList()

    Return Data
End Function

商家类

  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 {.MerchantID = p.MERCHANT_ID,
                                             .MerchantName = p.DESCRIPTION,
                                             .BranchID = r.INTERNAL_NUM,
                                             .BranchName = r.BRANCH_DESC})

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

错误

The error is Error Value of type '1-dimensional array of TTMS.App.WebSites.Data.Merchant' cannot be converted to 'System.Collections.Generic.List(Of TTMS.Web.WebSites.WCF.Merchant)'.

【问题讨论】:

  • 有两个问题:首先,当方法声明返回List(Of T)时,不能返回T[]数组。第二个:你的 Merchant 类命名空间不同。
  • TTMS.App.WebSites.Data.Merchant 不等于 TTMS.Web.WebSites.WCF.Merchant
  • 发布ws.GetMerchantList()的代码。
  • @KarlAnderson 我已经更新了代码,请看一下。谢谢

标签: asp.net vb.net wcf


【解决方案1】:

您需要可以将TTMS.App.WebSites.Data.Merchant 映射到TTMS.Web.WebSites.WCF.Merchant 的逻辑,即使它们是相同的,因为就 .NET 而言它们是两种不同的类型。

类似这样的:

Dim MapppedData As New List(Of Data.Merchant)
Dim Data = ws.GetMerchantList()

For Each theMerchant As WCF.Merchant In data
    ' Do logic here to map each of the fields in the 
    ' corresponding Merchant classes

    ' Add mapped Merchant (Data.Merchant) to the list

Next

' Return the mapped list of Data.Merchant objects
Return MappedData

更新:

我建议重命名 WCF 服务中的方法和类,以使它们与调用层中的不匹配,如下所示:

Public Class WsMerchant

End Class

Public Function GetMerchants() As List(Of WsMerchant)

End Function

现在,您的 WCF 服务层正在返回一个名称与尝试使用它的层不冲突的对象。因此,您的调用函数 (GetMerchantList()) 现在是这样的:

Public Function GetMerchantList() As List(Of Merchant) Implements IMerchant.GetMerchantList
    Dim ws As New aMerchantService.MerchantServiceClient

    Dim General As New General
    Dim kWSUrl As String = ""

    Dim endpointAddress = ws.Endpoint.Address

    Dim newEndpointAddress As New EndpointAddressBuilder(endpointAddress)
    kWSUrl = General.ConvertWsURL("App")

    newEndpointAddress.Uri = New Uri(kWSUrl & "MerchantService.svc")
    ws = New aMerchantService.MerchantServiceClient("BasicHttpBinding_IMerchantService", newEndpointAddress.ToEndpointAddress())

    Dim MapppedData As New List(Of Merchant)
    Dim Data = ws.GetMerchantList()

    For Each theMerchant As WsMerchant In data
        ' Do logic here to map each of the fields in the WsMerchant class 
        ' to the corresponding Merchant class fields
        Dim theMappedMerchant As New Merchant
        theMappedMerchant.Name = theMerchant.Name


        ' Add mapped Merchant (Merchant) to the list
        MappedData.Add(theMappedMerchant)
    Next

    ' Return the mapped list of Merchant objects
    Return MappedData
End Function

【讨论】:

  • 我已经尝试按照上述方法进行操作,但它不起作用。对不起,我是 WCF 开发的新手。它会很高兴你告诉我更多的细节。谢谢
  • @user3051461 - 您必须创建一个新的Merchant 对象并从WsMerchant 对象中获取属性值,我更新了答案,显示了如何映射两个Name 属性。在某些情况下,两者之间的属性名称可以匹配,而在其他情况下则不匹配。
猜你喜欢
  • 2011-09-25
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-09
  • 1970-01-01
相关资源
最近更新 更多