0

这是我的功能:

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

我收到一个错误:

类型的值System.Collections.Generic.List(Of <anonymous type>)不能转换为System.Collections.Generic.List(Of NorthwindModel.Employees)

4

1 回答 1

3

您在select 语句中实例化了一个Anonymous对象。尝试使用这个:

Return DB.Employees.ToList()

编辑:如果你想返回一个只有这 3 个属性的对象列表,你可以定义一个包含这些属性的类:

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

然后返回该新类的列表:

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
于 2012-05-04T14:12:57.203 回答