"Contains" lambda expression

.net dynamic-linq lambda

Question

I use a dynamic filter, that filters a collection using object properties, operators and values. Now, if the property is a string, the operator is "contains" and the value is "word", the filtered objects containing the "world" should be filtered accordingly.

In Linq I have the Expression.Equal, NotEqual, GreaterThanOrEqual, LessThanOrEqual

but don't have "Contains". How to replace it?

Consider the following code (in VB.NET, but does not matter)

  Select Case compOp
    Case ComparisonOperator.Contains
      ' ?????? WHAT HERE ???? '
      filterExpression = Expression.Lambda(Of Func(Of PropertyType, Boolean))(Expression.Equal(myObjPropertyParam, myConstExpression), myObjParam)

    Case ComparisonOperator.Different
      filterExpression = Expression.Lambda(Of Func(Of PropertyType, Boolean))(Expression.NotEqual(myObjPropertyParam, myConstExpression), myObjParam)

    Case ComparisonOperator.Equal
      filterExpression = Expression.Lambda(Of Func(Of PropertyType, Boolean))(Expression.Equal(myObjPropertyParam, myConstExpression), myObjParam)

    Case ComparisonOperator.GreatherThanOrEqual
      filterExpression = Expression.Lambda(Of Func(Of PropertyType, Boolean))(Expression.GreaterThanOrEqual(myObjPropertyParam, myConstExpression), myObjParam)

    Case ComparisonOperator.LessThanOrEqual
      filterExpression = Expression.Lambda(Of Func(Of PropertyType, Boolean))(Expression.LessThanOrEqual(myObjPropertyParam, myConstExpression), myObjParam)
  End Select

EDIT:

Used Solution, gratie to Thomas Levesque and Jon Skeet )

  Dim expr As Expression = Nothing

  Select Case compOp
    Case ComparisonOperator.Contains
      expr = Expression.Call(myObjPropertyParam, "Contains", Nothing, constantExpression)
    Case ComparisonOperator.Different
      expr = Expression.NotEqual(myObjPropertyParam, constantExpression)
    Case ComparisonOperator.Equal
      expr = Expression.Equal(myObjPropertyParam, constantExpression)
    Case ComparisonOperator.GreatherThanOrEqual
      expr = Expression.GreaterThanOrEqual(myObjPropertyParam, constantExpression)
    Case ComparisonOperator.LessThanOrEqual
      expr = Expression.LessThanOrEqual(myObjPropertyParam, constantExpression)
  End Select

  filterExpression = Expression.Lambda(Of Func(Of PropertyType, Boolean))(expr, myObjParam)
1
4
8/25/2011 6:39:04 PM

Accepted Answer

There is no Contains operator (although there are methods named Contains). It would only make sense for some types (like String, collections and a few others), but not for others; for instance how would you define the Contains operator for integers or dates ?

Now, if you want to apply the Contains method to a string via a lambda expression, you have to generate an expression that calls the method, using Expression.Call.

3
8/25/2011 5:57:25 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow