LINQ in VB InvalidCastException

时间:2010-06-24 04:32:57

标签: vb.net linq .net

我遇到了这个问题

System.InvalidCastException:无法转换类型为'System.Data.Linq.DataQuery 1[Student]' to type 'System.Collections.Generic.List 1 [Student]'的对象

调试此功能时:

Public Shared Function SearchStudent(ByVal firstname As String) As List(Of Student)

    Dim db As New DemoDataContext()
    Dim query = From st In db.Students _
                Where (st.FirstName.StartsWith(firstname)) _
                Select st

    Return CType(query, List(Of Student))


End Function

我的项目属性设置:Option explicit ON和Option strict ON

我想从查询中返回一个学生列表。谁能帮我?谢谢。

1 个答案:

答案 0 :(得分:3)

    Dim query = (From st In db.Students _
            Where (st.FirstName.StartsWith(firstname)) _
            Select st).ToList()