Home > database >  Get the name of the lambda expression with the index from the expression's array
Get the name of the lambda expression with the index from the expression's array

Time:10-07

I need to get expression name for query alias.

I need it:

Customer customer = null;

string expressionAdresses2Number = ExpressionUtils.PathString(() => customer.Adresses[2].Number);

And the result I would like was "Adresses[2].Number", but I could only get"Adresses.Number", I couldn't get the array part of the expression.

I tried this example: https://dotnetfiddle.net/eJ5nvl (based on this example: https://stackoverflow.com/a/22135756/2290538)

Does anyone have any suggestions how to get the expression part of the array with index selected "[2]"?

Model example:

public class Customer
{
    public int Id { get; set; }
    public Adress Adress { get; set; }
    public IList<Adress> Adresses { get; set; }
}

public class Adress
{
    public int Id { get; set; }
    public int Number { get; set; }
}

CodePudding user response:

The indexer is translated to a MethodCallExpression, so you need to override VisitMethodCall.

You can do a visitor like this:

public class PathVisitor : ExpressionVisitor
{

    // You should not use MemberInfo for the list type anymore, since the path can have indexer expressions now!
    // Here I convert both the indexer expressions and the member accesses to string in the visitor
    // You can do something more fancy, by creating your own class that represents either of these two cases
    // and then formatting them afterwards.
    internal readonly List<string> Path = new List<string>();

    protected override Expression VisitMember(MemberExpression node)
    {
        if (node.Member is PropertyInfo)
        {
            Path.Add(node.Member.Name);
        }
        return base.VisitMember(node);
    }
 
    protected override Expression VisitMethodCall(MethodCallExpression node) {
        if (node.Method.Name == "get_Item") { // name of the indexer

            // you can format this in a better way on your own. This is just an example
            Path.Add(node.Arguments.First().ToString());
        }
        return base.VisitMethodCall(node);
    }
}

Usage:

public static IReadOnlyList<string> Get(LambdaExpression expression)
{
    var visitor = new PathVisitor();
    visitor.Visit(expression.Body);
    visitor.Path.Reverse();
    return visitor.Path;
}

public static string PathString(Expression<Func<object>> expression)
{
    return string.Join(".", Get(expression));
}

This produces an output of Adresses.2.Number for your lambda.

Note that this assumes the lambdas are only with property accesses and indexers with constant arguments.

  • Related