开发者

Lambda Expression "Contains"

开发者 https://www.devze.com 2023-03-30 15:09 出处:网络
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

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), myObj开发者_如何学运维Param)

    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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消