我正在使用LinqKit库中的PredicateBuilder类在VB.net中构建谓词。
我的数据源是手动构建的数据表。
我发现的所有示例都显示了创建谓词的人,然后将该谓词作为参数传递给datatable.AsEnumerable()上的Where()
方法。
但intellisense告诉我,Where()
方法采用“System.Func
”类型的参数,但PredicateBuilder返回的类型为“System.Linq.Expressions.Expression(Of Func(Of T, Boolean))
”
我错过了什么?
示例:
Dim ds As DataTable = getData()
Dim tmp As IEnumerable(Of DataRow) = New DataTable().AsEnumerable()
' CREATE DYNAMIC LINQ WHERE CLAUSE
Dim predicate As System.Linq.Expressions.Expression(Of Func(Of DataRow, Boolean)) = PredicateBuilder.True(Of DataRow)()
If cbHPMS_ShowRequired.Checked Then
predicate = predicate.And(Function(x As DataRow) x("RECORD_TYPE") = "REQUIRED")
End If
If cbHPMS_ShowOptional.Checked Then
predicate = predicate.And(Function(x As DataRow) x("RECORD_TYPE") = "OPTIONAL")
End If
If cbHPMS_EmptyRecord.Checked Then
predicate = predicate.And(Function(x As DataRow) x("RECORD_STATUS") = "EMPTY")
End If
If cbHPMS_PartialRecord.Checked Then
predicate = predicate.And(Function(x As DataRow) x("RECORD_STATUS") = "PARTIAL")
End If
If cbHPMS_CompletedRecord.Checked Then
predicate = predicate.And(Function(x As DataRow) x("RECORD_STATUS") = "COMPLETE")
End If
If Not String.IsNullOrEmpty(ddHPMS_RoadName.SelectedValue) And Not ddHPMS_RoadName.SelectedValue.Equals("Select") Then
predicate = predicate.And(Function(x As DataRow) x("RoadName") = ddHPMS_RoadName.SelectedValue)
End If
tmp = ds.AsEnumerable().Where(predicate)
答案 0 :(得分:4)
我没有使用LinqKit但会认为它会像
tmp = ds.AsEnumerable().Where(predicate.Compile())