Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:42:28+00:00 2026-05-22T11:42:28+00:00

I’m trying to create a generic function to build a Linq expression from a

  • 0

I’m trying to create a generic function to build a Linq expression from a list of Telerik grid filters. I’m trying to stay clear of dynamic Linq. Because I don’t know the type that I’m building the expression for, I’m attempting to use reflection in the lambda functions to refer to a property, but my results are never filtered. The call to my helper function to get the property value is never called though it does hit the “Add” lines. Putting the reflection in line (such as the “GreaterThan” below) doesn’t make a difference. here is my code:

Public Shared Function buildRadFilter(Of T)(ByVal filterExpression As List(Of GridFilterExpression)) As Expressions.Expression(Of Func(Of T, Boolean))
        Dim returnPred = PredicateBuilder.True(Of T)()

        For Each exp As GridFilterExpression In filterExpression
            Dim iExp = exp
            Dim doDefault = False
            Select Case iExp.FilterFunction
                Case "Contains"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).Contains(iExp.FieldValue))
                Case "DoesNotContain"
                    returnPred.And(Function(x) Not DirectCast(propVal(x, iExp.FieldName), String).Contains(iExp.FieldValue))
                Case "StartsWith"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).StartsWith(iExp.FieldValue))
                Case "EndsWith"
                    returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).EndsWith(iExp.FieldValue))
                Case "EqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) = iExp.FieldValue)
                Case "NotEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <> iExp.FieldValue)
                Case "GreaterThan"
                    'returnPred.And(Function(x) propVal(x, iExp.FieldName) > iExp.FieldValue)
                    returnPred.And(Function(x) x.GetType().GetProperty(iExp.FieldName).GetValue(x, Nothing))
                Case "GreaterThanOrEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) >= iExp.FieldValue)
                Case "LessThan"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) < iExp.FieldValue)
                Case "LessThanOrEqualTo"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <= iExp.FieldValue)
                Case "IsEmpty"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) = "")
                Case "NotIsEmpty"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) <> "")
                Case "IsNull"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) Is Nothing)
                Case "NotIsNull"
                    returnPred.And(Function(x) propVal(x, iExp.FieldName) IsNot Nothing)
                Case "Between"
                    Dim vals As String() = iExp.FieldValue.Split(" ")
                    If vals.Length > 1 Then
                        returnPred.And(Function(x) propVal(x, iExp.FieldName) >= vals(0) AndAlso propVal(x, iExp.FieldName) <= vals(1))
                    Else
                        doDefault = True
                        Exit Select
                    End If
                Case "NotBetween"
                    Dim vals As String() = iExp.FieldValue.Split(" ")
                    If vals.Length > 1 Then
                        returnPred.And(Function(x) propVal(x, iExp.FieldName) < vals(0) OrElse propVal(x, iExp.FieldName) > vals(1))
                    Else
                        doDefault = True
                        Exit Select
                    End If
                Case Else
                    doDefault = True
                    Exit Select
            End Select
            If doDefault Then
                returnPred.And(Function(x) DirectCast(propVal(x, iExp.FieldName), String).StartsWith(iExp.FieldValue))
            End If
        Next

        Return returnPred
    End Function

    'only works for scalar values
    Public Shared Function propVal(ByRef obj As Object, ByVal name As String) As Object
        Return obj.GetType().GetProperty(name).GetValue(obj, Nothing)
    End Function

Thank you in advance for any help you may be able to offer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:42:29+00:00Added an answer on May 22, 2026 at 11:42 am

    The reason why is the And predicate creates a new expression which you are not saving. It doesn’t modify the existing one in place. You need to switch every returnPred.And line to

    returnPred = returnPred.And(...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.