2013-12-11 66 views
0

嗨我在將數組轉換爲通用列表時遇到了問題。它引發錯誤如下。在班上我已經以ToList的形式返回,但爲什麼我仍然會出現。我已經將服務引用返回類型更改爲泛型列表默認情況下是數組,但它獲取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)'. 
+0

有兩個問題:首先,你不能返回一個'T []數組'當聲明方法返回'名單(共T) '。第二個:你的'Merchant'類命名空間不同。 – MarcinJuraszek

+0

'TTMS.App.WebSites.Data.Merchant'不等於'TTMS.Web.WebSites.WCF.Merchant'。 –

+0

發佈'ws.GetMerchantList()'的代碼。 –

回答

0

你需要能夠在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 

UPDATE:

我建議重命名你的WCF服務的方法和類,使它們不匹配那些在您的通話層,這樣:

Public Class WsMerchant 

End Class 

Public Function GetMerchants() As List(Of WsMerchant) 

End Function 

現在你的WCF服務層正在返回一個沒有衝突的對象t正在嘗試使用它的圖層。因此,你的調用函數(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 
+0

我已經嘗試做如上,但它不工作。對不起,我是WCF開發新手。很高興你向我展示更多細節。 Thx – user3051461

+0

@ user3051461 - 已更新的答案。 –

+0

我得到錯誤轉換Data.Merchant WCF.Merchant – user3051461