How to make an Expression tree that works similarly to "StartsWith"

.net c# expression-trees lambda vb.net

Question

Right now, I use this technique to compare two integers.

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 functions properly and is used in this manner.

query = ETForGreaterThan(query, Value, propertyInfo)

As you can see, it adds a where clause based on a property and a value after receiving an IQueryable collection from me. Y may create analogues of Lessthan, LessOrEqualThan, etc. as System. Linq. Expressions. This set of operators is predefined in expression.

How can I modify this code to operate on strings the same way? System.Linq. Expressions. I'm a total beginner with Expression trees and it doesn't provide me with a predefined operator like "contains" or "startwith."

Please provide your response in C# or VB, and thanks. Select the one that gives you the most comfort.

1
8
12/29/2010 2:29:52 PM

Accepted Answer

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);
        }
    }
}
20
12/29/2010 1:27:12 PM

Popular Answer

Since it is a method rather than an operator, you may call it using Expression. Call() with typeof as the methodinfo argument (string). GetMethod("StartsWith").



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