VB.NET ASP.NET从数据库中检索一个conutries列表

时间:2016-05-12 10:11:54

标签: 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();
    }
}

这是我尝试转换为VB.net的C#代码示例,但我不断收到此错误消息,无法找出原因。

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

非常感谢任何帮助。谢谢!

2 个答案:

答案 0 :(得分: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

结束功能

您在from语句中选择了Counties而不是Countries。

答案 1 :(得分: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