Substitute a MemberExpression by another Domain Specific Expression

14 views
Skip to first unread message

Jordi Cabré

unread,
Mar 10, 2016, 8:28:21 AM3/10/16
to re-motion Users
I'm trying to transform a Linq Expression on WhereClause to another domain specific expression.

This is Linq sentence I'm trying to analyze:

            IEnumerable<TClass> classes = Provider.LivingQueryFactory.CreateLinqQuery<TClass>().Where(tc => tc.Name.EndsWith(string.Empty));
            var projectedClasses = from tc in classes select new { PrimaName = tc.Name, SecondName = tc.Name };

            foreach (var c in projectedClasses)
            {
                Console.WriteLine(c.PrimaName);
            }

So, WhereClause contains: tc => tc.Name.EndsWith(string.Empty).

I'm trying to set tc.Name to a DSLFieldExpression that I want it contains the fieldname = "Name" and fieldType = String:

    internal class DSLFieldExpression : Expression
    {
        private readonly string fieldName;
        private Type fieldType;
    }

I've created a ExpressionVisitor inherits from ReLinqExpressionVisitor in order to visit a member:

    internal class QuerySourceReferencePropertyTransformingTreeVisitor : RelinqExpressionVisitor
    {
        private MemberExpression parent;
        private DSLFieldExpression queryField;

        protected override Expression VisitMember(MemberExpression expression)
        {
            parent = expression;

            var result = base.VisitMember(expression);

            return queryField ?? result;
        }

        protected override Expression VisitQuerySourceReference(QuerySourceReferenceExpression expression)
        {
            var propertyInfo = parent.Member as PropertyInfo;

            if (propertyInfo == null)
            {
                throw new NotSupportedException("Only MemberExpression of type PropertyInfo may be used on QuerySourceReferenceExpression.");
            }

            var propertyType = propertyInfo.PropertyType;
            if (propertyType.IsEnum)
            {
                propertyType = Enum.GetUnderlyingType(propertyType);
            }

            queryField = new DSLFieldExpression (propertyInfo.Name, propertyType);
            return base.VisitQuerySourceReference(expression);
        }
    }

However, VisitQuerySourceReference is never performed.
Any ideas?
Reply all
Reply to author
Forward
0 new messages