使用EF + Lambda表达式的动态查询

时间:2011-08-09 18:42:57

标签: .net vb.net

尝试根据用户选择(报告)的参数构建linq查询但是它没有正常运行...加载整个表而不是请求的过滤结果集。这是我正在使用的代码:

Public Sub GetResults()
    Using ctx As New ReportingEntities()

        Dim results As IQueryable(Of Orders) = ctx.Orders

        ' get reason params
        If lbReason.GetSelectedIndices().Count() > 0 Then
            Dim reasons = (From i As ListItem In lbReason.Items
                           Where i.Selected
                           Select Integer.Parse(i.Value)).ToList()

            If reasons IsNot Nothing Then
                Dim where =
                        CommonDataUtils.BuildOrExpressionTree(Of Orders, Integer)(reasons,
                                                                                                Function(r) r.ID)
                results.Where(where)
            End If
        End If

        ' get commodity params
        If lbCommodity.GetSelectedIndices().Count() > 0 Then
            Dim comms = (From i As ListItem In lbCommodity.Items
                           Where i.Selected
                           Select i.Value).ToList()

            If comms IsNot Nothing Then
                Dim where =
                    CommonDataUtils.BuildOrExpressionTree(Of Orders, String)( _
                        DirectCast(comms, IEnumerable(Of String)),
                        Function(r) r.Affected_Commodity)
                results.Where(where)
            End If
        End If

        ' get disposition params
        If lbDisposition.GetSelectedIndices().Count() > 0 Then
            Dim disps = (From i As ListItem In lbDisposition.Items
                       Where i.Selected
                       Select Integer.Parse(i.Value)).ToList()

            If disps IsNot Nothing Then
                Dim where =
                    CommonDataUtils.BuildOrExpressionTree(Of Orders, Integer)( _
                                                    disps,
                                                    Function(r) r.ID)
                results.Where(where)
                filter.Add(where)
            End If
        End If


        ' get facility params
        If lbProdFacility.GetSelectedIndices().Count() > 0 Then
            Dim facs = (From i As ListItem In lbProdFacility.Items
                       Where i.Selected
                       Select Integer.Parse(i.Value)).ToList()

            If facs IsNot Nothing Then
                Dim where =
                        CommonDataUtils.BuildOrExpressionTree(Of Orders, Integer)(facs,
                                                                            Function(r) r.ID)
                results.Where(where)
                filter.Add(where)
            End If
        End If

        ' get customer number params
        Dim custnums As List(Of Integer) = If(ViewState("cust_nums") Is Nothing, Nothing, ViewState("cust_nums"))
        If custnums IsNot Nothing AndAlso custnums.Count > 0 Then
            Dim where = CommonDataUtils.BuildOrExpressionTree(Of Orders, Integer) _
                        (custnums, Function(i) i.ID)
            results.Where(where)
            filter.Add(where)
        End If

        ' get reason code params
        Dim resnums As List(Of Integer) = If(ViewState("res_nums") Is Nothing, Nothing, ViewState("res_nums"))
        If resnums IsNot Nothing AndAlso resnums.Count > 0 Then
            Dim where = CommonDataUtils.BuildOrExpressionTree(Of Orders, Integer) _
                        (resnums, Function(i) i.ID)
            results.Where(where)
            filter.Add(where)
        End If

        ' get date params
        If IsDate(startDate.Value.Trim()) And IsDate(endDate.Value.Trim()) Then
            Dim sdate As DateTime
            Dim edate As DateTime
            DateTime.TryParse(startDate.Value.Trim(), sdate)
            DateTime.TryParse(endDate.Value.Trim(), edate)


            If edate > sdate Then
                Dim dateWhere As Expression(Of Func(Of Orders, Boolean)) =
                    Function(d) (d.Date_Rejected >= sdate And d.Date_Rejected <= edate)
                results.Where(dateWhere)
                filter.Add(dateWhere)
            End If



        ' populate results grid
        grd.DataSource = results
        grd.DataBind()

    End Using
End Sub

1 个答案:

答案 0 :(得分:1)

LINQ查询是不可变的,就像字符串一样。记住这是怎么回事:

s + " world"

...实际上并没有改变s的价值?它与LINQ查询的工作方式相同。

results.Where(where)

...不会更改查询。您可能正在寻找:

results = results.Where(where)