PredicateBuilder、VB.net 和Where()

发布于 2024-11-17 17:01:41 字数 1738 浏览 3 评论 0原文

我正在使用 LinqKit 库中的 PredicateBuilder 类在 VB.net 中构建谓词。 我的数据源是手动构建的数据表。 我找到的所有示例都显示人们创建谓词,然后将该谓词作为参数传递给 datatable.AsEnumerable() 上的 Where() 方法。

但智能感知告诉我 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)

I am building a predicate in VB.net using the PredicateBuilder class from the LinqKit library.
My datasource is a manually constructed datatable.
All the examples I've found show folks creating the predicate and then passing that predicate as an argument to the Where() method on the datatable.AsEnumerable().

But intellisense is telling me that the Where() method takes a paremeter of type "System.Func", but the type returned by PredicateBuilder is "System.Linq.Expressions.Expression(Of Func(Of T, Boolean))"

What am I missing?

Example:

        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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开半夏魅人心 2024-11-24 17:01:41

我没有使用过 LinqKit 但认为它会像

tmp = ds.AsEnumerable().Where(predicate.Compile())

I have not used LinqKit but would think it would be like

tmp = ds.AsEnumerable().Where(predicate.Compile())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文