2016-05-12 40 views
1

我是ASP.NET新手,似乎無法找到我的問題的答案。VB.NET ASP.NET檢索列表的數據庫

我想在我的數據庫中檢索一個表格列表。

我發現了一種在C#中做到這一點的方法,但我需要在Viusal Basic中做到這一點。

private List<Country> PopulateCountry() 
{ 
    using (MyDatabaseEntities dc = new MyDatabaseEntities()) 
    { 
     return dc.Countries.OrderBy(a => a.CountryName).ToList(); 
    } 
} 

這是從C#代碼示例中,我試圖轉換成VB.net,但我不斷收到此錯誤信息,並且不能找出原因。

Private Function GetListOfCountries() As List(Of Country) 
    Using dc As New MyDatabaseEntities 
     Try 
      Dim countryList = (From p In dc.Counties Order By p.CountryName 
           Ascending 
           Select p) 
      Return countryList.ToList() 
     Catch ex As Exception 
      Return Nothing 
     End Try 
    End Using 

End Function 

Return countryList.ToList()線我得到這個錯誤

Value of type 'List(Of Country)' cannot be converted to 'List(Of Country)'. 

Error picture

任何幫助將非常感激。謝謝!

+1

你有一個錯字,請檢查您的圖片 - '名單(共縣)'VS'名單(國家)' – Fabio

回答

0

我相信這個功能將滿足您的需求。 (轉換失敗,原因是County而不是Country,其中County恰好是一個有效的實體類型爲好。)

Private Function GetListOfCountries() As List(Of Country) 
    Using dc As New MyDatabaseEntities 
     Try 
      Return dc.Countries.OrderBy(Function(a) a.CountryName).ToList() 
     Catch ex As Exception 
      Return Nothing 
     End Try 
    End Using 

End Function 
+1

是的錯字造成了我所有的問題,它從我創建數據庫和模型開始。感謝Malcor和Lajos指出。 – newtothis

0
Private Function GetListOfCountries() As List(Of Country) 
Using dc As New MyDatabaseEntities 
    Try 
     Dim countryList = (From p In dc.Countries Order By p.CountryName 
          Ascending 
          Select p) 
     Return countryList.ToList() 
    Catch ex As Exception 
     Return Nothing 
    End Try 
End Using 

端功能

您是從聲明中選擇縣,而不是國家在你的。