I currently have this method for comparing 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 great and is consumed this way.
query = ETForGreaterThan(query, Value, propertyInfo)
As you can see, I give it the IQueryable collection and add the where clause to it based on the property and value. Y can build Lessthan, LessOrEqualThan, etc. Equivalents like System.Linq.Expressions.Expression have these operators predefined.
ยฟHow can I convert this code to do the same with strings? System.Linq.Expressions.Expression does not give me a predefined operator like "contains" or "startwith", and I am really noob with expression trees.
Thanks, and please write your answer in C # / VB. Choose the one that makes you feel more comfortable.
Jonathan
source share