locked
How to perform case insenitive search in linq RRS feed

  • Question

  • User2057255475 posted

    Hi,

    I have implemented 'Like' filtering in asp net dynamic data like below.

    Protected Function Query(ByVal searchTerm As String, ByVal source As IQueryable) As MethodCallExpression
            Dim searchFilter As ConstantExpression = Nothing
            Dim parameter As ParameterExpression = Expression.Parameter(source.ElementType)
            Dim prop As MemberExpression = Expression.Property(parameter, Column.Name)
    
            Dim method As MethodInfo = Nothing
            Dim methodExp = Nothing
            Dim expLambda = Nothing
            If Nullable.GetUnderlyingType(prop.Type) IsNot Nothing Then
                prop = Expression.Property(prop, "Value")
            End If
            If searchTerm.StartsWith("%") And searchTerm.EndsWith("%") Then
                searchFilter = Expression.Constant(searchTerm.Replace("%", ""))
                method = GetType(String).GetMethod("Contains", New Type() {GetType(String)})
                methodExp = Expression.Call(prop, method, searchFilter)
            ElseIf searchTerm.StartsWith("%") Then
                searchFilter = Expression.Constant(searchTerm.Replace("%", ""))
                method = GetType(String).GetMethod("EndsWith", New Type() {GetType(String)})
                methodExp = Expression.Call(prop, method, searchFilter)
            ElseIf searchTerm.EndsWith("%") Then
                searchFilter = Expression.Constant(searchTerm.Replace("%", ""))
                method = GetType(String).GetMethod("StartsWith", New Type() {GetType(String)})
                methodExp = Expression.Call(prop, method, searchFilter)
            ElseIf searchTerm.Contains("%") Then
                Dim index As Integer = searchTerm.IndexOf("%")
                Dim leftString As ConstantExpression = Expression.Constant(searchTerm.Substring(0, index))
                Dim rightString As ConstantExpression = Expression.Constant(searchTerm.Substring(index + 1))
    
                methodExp = Expression.And(Expression.Call(prop, GetType(String).GetMethod("StartsWith", New Type() {GetType(String)}), leftString), Expression.Call(prop, GetType(String).GetMethod("EndsWith", New Type() {GetType(String)}), rightString))
               
            End If
            expLambda = Expression.Lambda(methodExp, parameter)
            Dim resultExpression As MethodCallExpression = Expression.[Call](GetType(Queryable), "Where", New Type() {source.ElementType}, New Expression() {source.Expression, Expression.Quote(expLambda)})
            Return resultExpression
        End Function

    The above code is working fine if i provide the value in required case like 'C%' or 'c%'. Is is possible to make it case insensitive something like, if i provide 'c%' or 'C%' it should return the same result.

    The comparison in database should be like Upper(columnname) like upper('c%')

    Thanks,

    Praveen

    Friday, October 18, 2013 3:31 AM

Answers

All replies