score:1

Accepted answer

you are very close with your 2nd approach!

let's say you define the projection of the product entity to the dto (the mapper as you call it) like you did:

expression<func<product, productdto>> productprojection = prod => new productdto
{
    id = prod.id,
    dateoftransaction = prod.date
    // ...
};

and the projection of the client entity to it's dto like this (slightly simpler, but logically equivalent to what you did):

expression<func<client, clientdto>> clientprojection = client => new clientdto
{
    id = client.clientid,
    firstname = client.firstname,
    // ...
    products = client.products.select(productprojection.compile()).tolist(),
    // ...
};

the compiler let's you do so, but the queryable will not understand that. however what you have achieved is that the productprojection is somehow contained in the expression tree. all you have to do is some expression manipulation.

if you look at the subtree the compiler builds for the argument to .select you'll find a methodcallexpression - the call to .compile(). it's .object expression - the thing that is to be compiled - is a memberexpression accessing a field named productprojection(!) on an constantexpression containing an instance of an oddly named compiler generated closure class.

so: find .compile() calls and replace them with what would be compiled, ending up with the very expression tree you had in your original version.

i'm maintaining a helper class for expression stuff called express. (see another answer that deals with .compile().invoke(...) for a similar situation).

clientprojection = express.uncompile(clientprojection);
var clientlist = dbclients.select(clientprojection).tolist();

here's the relevant snipped of the express class.

public static class express
{
    /// <summary>
    /// replace .compile() calls to lambdas with the lambdas themselves.
    /// </summary>
    public static expression<tdelegate> uncompile<tdelegate>(expression<tdelegate> lambda)
    => (expression<tdelegate>)uncompilevisitor.singleton.visit(lambda);

    /// <summary>
    /// evaluate an expression to a value.
    /// </summary>
    private static object getvalue(expression x)
    {
        switch (x.nodetype)
        {
            case expressiontype.constant:
                return ((constantexpression)x).value;
            case expressiontype.memberaccess:
                var xmember = (memberexpression)x;
                var instance = xmember.expression == null ? null : getvalue(xmember.expression);
                switch (xmember.member.membertype)
                {
                    case membertypes.field:
                        return ((fieldinfo)xmember.member).getvalue(instance);
                    case membertypes.property:
                        return ((propertyinfo)xmember.member).getvalue(instance);
                    default:
                        throw new exception(xmember.member.membertype + "???");
                }
            default:
                // note: it would be easy to compile and invoke the expression, but it's intentionally not done. callers can always pre-evaluate and pass a member of a closure.
                throw new notsupportedexception("only constant, field or property supported.");
        }
    }

    private sealed class uncompilevisitor : expressionvisitor
    {
        public static uncompilevisitor singleton { get; } = new uncompilevisitor();
        private uncompilevisitor() { }

        protected override expression visitmethodcall(methodcallexpression node)
        {
            if (node.method.name != "compile" || node.arguments.count != 0 || node.object == null || !typeof(lambdaexpression).isassignablefrom(node.object.type))
                return base.visitmethodcall(node);
            var lambda = (lambdaexpression)getvalue(node.object);
            return lambda;

            // alternatively recurse on the lambda if it possibly could contain .compile()s
            // return visit(lambda); // recurse on the lambda
        }
    }
}

score:1

use linqkit to expand user-defined lambda functions into the lambdas needed in the query:

https://github.com/scottksmith95/linqkit


Related Query

More Query from same tag