13

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 System.Linq.Expressions.Expression has this operators predefined.

¿How can I transform this code to do the same with strings? System.Linq.Expressions.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.

AakashM
  • 62,551
  • 17
  • 151
  • 186
Jonathan
  • 11,809
  • 5
  • 57
  • 91

2 Answers2

24
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            using (var context = new NorthwindEntities())
            {
                PropertyInfo propertyInfo = typeof(Customer).GetProperty("CustomerID"); 

                IQueryable<Customer> query = context.Customers;
                query = ETForStartsWith<Customer>(query, "A", propertyInfo); 
                var list = query.ToList();
            }
        }

        static IQueryable<T> ETForStartsWith<T>(IQueryable<T> query, string propertyValue, PropertyInfo propertyInfo)
        {
            ParameterExpression e = Expression.Parameter(typeof(T), "e");
            MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
            ConstantExpression c = Expression.Constant(propertyValue, typeof(string));
            MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
            Expression call = Expression.Call(m, mi, c);

            Expression<Func<T, bool>> lambda = Expression.Lambda<Func<T, bool>>(call, e); 
            return query.Where(lambda);
        }
    }
}
Tom Brothers
  • 5,929
  • 1
  • 20
  • 17
5

It's not an operator, but a method, so you can call it with Expression.Call(), where the methodinfo parameter will be typeof(string).GetMethod("StartsWith").

fejesjoco
  • 11,763
  • 3
  • 35
  • 65
  • I'm reading about it, but I didn't use expression-trees before. ¿Could you provide me an example please? – Jonathan Dec 29 '10 at 12:35