2016-05-12 42 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 

端功能

您是从声明中选择县,而不是国家在你的。