Question

Dim Param = Expression.Parameter(source.ElementType)
Dim columnProperty = Expression.PropertyOrField(Param, Column.Name)
Dim conversion As Expression = Expression.Convert(columnProperty, GetType(String))
Dim likeValue = Expression.Constant(value, GetType(String))
Dim ContainMethodExp = Expression.[Call](columnProperty, GetType(String).GetMethod("Contains"), likeValue)
Dim where = Expression.[Call](GetType(Queryable), "Where", New Type() {source.ElementType}, source.Expression, Expression.Lambda(ContainMethodExp, Param))

This code works for the columns types string but it does not works for the int32 column type, I tried to use expression.convert to convert int32 to string but it fails. I am able to convert the member expression to object type but linq to entities supports only the Primitive Data Types.

Please help to write the same lambda expression for Int 32 type for dynamic filtering

Was it helpful?

Solution

You expression tree is equivalent to:

.Where(x => x.Column.Contains(likeValue"))

How would you like to make Contains/LIKE comparison with integers?!

For integers, you should use Expression.Equal instead of Contains method call:

Dim Param = Expression.Parameter(source.ElementType)
Dim columnProperty = Expression.PropertyOrField(Param, Column.Name)

Dim equalValue = Integer.Parse(value)

Dim equalValueExpression = Expression.Constant(equalValue, GetType(Integer))
Dim equalExpression = Expression.Equal(columnProperty, equalValueExpression )
Dim where = Expression.[Call](GetType(Queryable), "Where", New Type() {source.ElementType}, source.Expression, Expression.Lambda(equalExpression , Param))

I haven't tested that, but should work.

OTHER TIPS

MarcinJuraszek answers works but I found another link which also helped me to solve the problem

Visit How to Type Cast dynamically to string while using Contains in Dynamic LINQ?

Another solution is to use StringConvert of SQL functions so that you can use contains

    Dim method As MethodInfo = GetType(String).GetMethod("Contains", New Type() {GetType(String)})
    Dim Value As Expression = Expression.Convert(searchFilter, GetType(String))
    Dim Param = Expression.Parameter(source.ElementType)
    Dim columnProperty As Expression = Expression.Property(Param, Column.Name)
    columnProperty = Expression.Convert(columnProperty, GetType(System.Nullable(Of Double)))
    Dim stringConvertMethod = GetType(SqlFunctions).GetMethod("StringConvert", New Type() {GetType(System.Nullable(Of Double))})
    columnProperty = Expression.[Call](stringConvertMethod, columnProperty)
    Dim containsMethodExp = Expression.Call(columnProperty, method, Value)
    Dim likeValue = Expression.Constant(value, GetType(String))
    Dim ContainMethodExp = Expression.[Call](columnProperty, GetType(String).GetMethod("Contains"), likeValue)
    Dim where = Expression.[Call](GetType(Queryable), "Where", New Type() {source.ElementType}, source.Expression, Expression.Lambda(ContainMethodExp, Param))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top