通过LINQ向树添加节点会在运行时创建“不支持查询运算符”

时间:2008-11-03 21:53:27

标签: vb.net linq linq-to-sql

我正试图解决LINQ问题。最终目标是使用从LINQ to SQL(视图)获取的数据加载树视图。问题是当我尝试访问所获取的数据时,它不断抛出“在运行时不支持的查询运算符”。涉及的代码不过是:

    Dim tasklistdata As New lqDataContext
    Dim UserID As Integer

    UserID = (From vwUser In tasklistdata.Operators _
             Where vwUser.UserName Is Login.txtUsername.Text _
             Select vwUser.OperatorID).Single

    Dim lqHospitalList = From vwHospitalList In tasklistdata.SPM_Accounts _
                          Where vwHospitalList.OperatorID = UserID _
                          Order By vwHospitalList.CustomerName, vwHospitalList.Class _
                          Select vwHospitalList.CustomerName, vwHospitalList.Class, vwHospitalList.ClassCount, vwHospitalList.Charges

    tvHospitalSelect.Nodes.Add(lqHospitalList(0).CustomerName)

如果我要将代码更改为lqHospitalList.First.CustomerName,它会按预期工作,但是将其更改为数组位置是问题出现的地方。请指导我的逻辑缺陷。

1 个答案:

答案 0 :(得分:3)

Linq查询不是列表,因此它们没有索引。要迭代所有项目,您可以像这样使用foreach:

For Each hospital In lqHospitalList
    tvHospitalSelect.Nodes.Add(hospital.CustomerName)
Next

如果要将查询转换为列表:

lqHospitalList.ToList

或数组:

lqHospitalList.ToArray