我有一个VB.NET函数来获取基于状态参数的抱怨列表。它正在使用LINQ方法并且工作正常:
Public Function getComplain(ByVal stat As Integer) As List(Of ComplainTable)
Dim listt = (From tbl In context.ComplainTable _
Where tbl.STATUS = stat _
Select tbl).ToList
Return listt
End Function
问题是我想将LINQ查询更改为DataSet查询。我尝试的代码如下,但我得到空的结果/输出:
Public Function getComplain(ByVal stat As Integer) As List(Of ComplainTable)
Dim ut As New Util
Dim query = "SELECT * FROM ComplainTable"
Dim ds As DataSet = ut.getData(query)
Dim dt As DataTable = ds.Tables("SystemComplain") '???
Dim dr As IEnumerable(Of SystemComplain) = dt.AsEnumerable.Select("STATUS = " & stat) '???
Return dr.ToList
End Function
标有???的行是我不确定我做得正确的部分。我是.NET中的新手,需要在互联网上进行大量搜索才能完成基本工作。
更新1
Public Function getComplain(ByVal stat As Integer) As List(Of ComplainTable)
Dim ut As New Util
Dim query = "SELECT * FROM ComplainTable"
Dim ds As DataSet = ut.getData(query)
Dim dt As DataTable = ds.Tables(0)
Dim dr As IEnumerable(Of ComplainTable) = _
dt.AsEnumerable. _
Where(Function(x) x.Field(Of String)("STATUS") = stat). _
Select(Function(x) New ComplainTable With { _
.STATUS = x.Field(Of String)("STATUS") _
} _
)
Return dr.ToList
End Function
答案 0 :(得分:0)
您最初不能通过名称引用DataTable,因为您没有提供名称。但是你可以使用索引。
Dim dt As DataTable = ds.Tables(0)
至于过滤,而不是使用.Select,尝试。在第二行上的。所在。
答案 1 :(得分:0)
带有问号的第一行代码看起来不错,但如果这对您没有用,请尝试使用其他答案中建议的索引。无论如何,对于下一行,使用Enumerable.Where()
方法和正确的谓词表达式:
Dim dr As IEnumerable(Of DataRow) = dt.AsEnumerable.
Where(Function(x) x.Field(Of string)("STATUS") = stat)
供参考:Queries in LINQ to DataSet
更新:
Dim dr As IEnumerable(Of ComplainTable) = _
dt.AsEnumerable.
Where(Function(x) x.Field(Of string)("STATUS") = stat).
Select(Function(x) New ComplainTable With {
.STATUS = x.Field(Of string)("STATUS")
'TODO: manually populate other properties here'
}
)
}