How to create and Expression tree to do the same as "StartsWith"
Posted
by
Jonathan
on Stack Overflow
See other posts from Stack Overflow
or by Jonathan
Published on 2010-12-29T11:51:29Z
Indexed on
2010/12/29
11:53 UTC
Read the original article
Hit count: 187
Hi to all.
Currently, I have this method to compare two numbers
Private Function ETForGreaterThan(ByVal query As IQueryable(Of T), ByVal propertyValue As Object, ByVal propertyInfo As PropertyInfo) As IQueryable(Of T)
Dim e As ParameterExpression = Expression.Parameter(GetType(T), "e")
Dim m As MemberExpression = Expression.MakeMemberAccess(e, propertyInfo)
Dim c As ConstantExpression = Expression.Constant(propertyValue, propertyValue.GetType())
Dim b As BinaryExpression = Expression.GreaterThan(m, c)
Dim lambda As Expression(Of Func(Of T, Boolean)) = Expression.Lambda(Of Func(Of T, Boolean))(b, e)
Return query.Where(lambda)
End Function
It works fine and is consumed in this way
query = ETForGreaterThan(query, Value, propertyInfo)
As you can see, I give it an IQueryable collection and it add a where clause to it, base on a property and a value. Y can construct Lessthan, LessOrEqualThan etc equivalents as Expression has this operators predefined.
¿How can I transform this code to do the same with strings? Expression don't give me a predefined operator like "contains" or "startwith" and I'm really noob with Expression trees.
Thanks, and please Post your answer in C#/VB. Choose the one that make you feel more confortable.
© Stack Overflow or respective owner