vb.net - 通过 LINQ 向树添加节点在运行时创建 "query operator not supported"

标签 vb.net linq linq-to-sql

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

    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 它会按预期工作,但是将其更改为数组位置是问题出现的地方。请指导我指出我的逻辑缺陷所在。

最佳答案

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

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

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

或数组:
lqHospitalList.ToArray

关于vb.net - 通过 LINQ 向树添加节点在运行时创建 "query operator not supported",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/260116/

相关文章:

c# - 使用 linq to txt 解析行

c# - 如何使用多个、嵌套、transactionscope?

linq - 如何从 LINQ to SQL 中的子集合中删除记录?

xml - 使用 XSLT 重命名节点

vb.net - 如何检查 VB.NET 中缺少的引用?

vb.net - 如何在 Vb.Net 中使用 streamwriter 从字符串变量创建 csv 文件?

c# - Linq - 用引号聚合字符串

database - 在 VB.NET 中初始化数据集

c# - 在 Linq 中比较

sql-server - 在 LINQ 中选择具有奇怪值 @p__linq__0