2012-05-04 3 views
0

これは私の関数である:Entity Framework(linq)で選択したデータのリスト?

Public Function GetAllEmployee() As List(Of Employees) 
    Return DB.Employees.Select(Function(q) New With {q.EmployeeID, q.LastName,q.FirstName}).ToList() 
End Function 

私はエラーを取得しています:

Value of type System.Collections.Generic.List(Of <anonymous type>) cannot be converted to System.Collections.Generic.List(Of NorthwindModel.Employees) .

答えて

3

あなたは、SELECT文の内側Anonymousオブジェクトをインスタンス化しています。これを使用してみてください:

Return DB.Employees.ToList() 

EDIT:次に返す

Public Class MyEmployees 
    Public Property EmployeeId As Long 
    Public Property FirstName As String 
    Public Property LastName As String 
End Class 

:あなただけのものを3つのプロパティを持つオブジェクトのリストをお返ししたい場合は、これらのプロパティを含むクラスを定義することができますその新しいクラスのリスト:

Public Function GetAllEmployee() As List(Of MyEmployees) 
    Return DB.Employees.Select(Function(q) New MyEmployees With { .EmployeeId = q.EmployeeID, .LastName = q.LastName, .FirstName = q.FirstName}).ToList() 
End Function 
+0

この場合、「選択」を完全に削除することができます。 – Slauma

+0

@Slauma - ありがとう。編集された答え。 – CAbbott

+0

しかし私は3つのフィールド(employeeid、fname、lname)だけを欲しいです。 –

関連する問題