无法转换类型的对象(Linq To Object)

时间:2017-02-04 07:19:08

标签: linq-to-objects

   Function searchByName(lName As String) As List(Of Employees)

        Dim employees As New Employees
        Dim employeeList As New List(Of Employees)
        Dim resultList As New List(Of Employees)
        employeeList = employees.getEmployeeObject

        resultList = From employeesObj In employeeList
                     Where employeesObj.LastName = lName
                     Select employeesObj.EmployeeID
        Return resultList
    End Function

1 个答案:

答案 0 :(得分:0)

大多数Linq方法的结果(也适用于此处使用的内置理解语法)是IEnumerable(Of T)而不是List(Of T)。为了返回表达式的结果,您需要将结果放入列表中,最常见的方法是在生成的ToList引用上调用IEnumerable(Of Employees)扩展方法。< / p>

例如:

resultList = From employeesObj In employeeList
             Where employeesObj.LastName = lName
             Select employeesObj.EmployeeID
Return resultList.ToList()