2016-05-12 3 views
1

私はASP.NETを初めて使用しているため、私の問題の答えが見つからないようです。VB.NET ASP.NET concitriesフォームデータベースのリストを取得します。

私のデータベースのテーブルの国のリストを取得したいと思います。

私はC#でそれを行う方法を見つけましたが、私はViusal Basicでそれを行う必要があります。

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

これは、私はVB.netに変換しようとしたのC#からのコードサンプルですが、私はこのエラーメッセージを取得しておくと理由を把握することはできません。 Return countryList.ToList()行で

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 

私はすべてのヘルプははるかに高く評価されるだろう

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

Error picture

このエラーが発生します。ありがとう!あなたは、あなたからの声明の中で郡の代わりに国を選択している

+1

あなたがタイプミスを持って、あなたの写真を確認してください - (郡) 'リスト'(一覧 '対国の) ' – 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 

エンド機能

関連する問題