let there be :
Expression<Func<Message, bool>> exp1 = x => x.mesID == 1;
Expression<Func<MessageDTO, bool>> exp2 = x => x.mesID == 1;
now i need to pass exp1 to _db.Messages.where(exp1);
problem is i only have exp2, i need to convert the type to Message , All properties are the same !
now i do this :
var par = Expression.Parameter(typeof(Message));
var ex = (Expression<Func<Message, bool>>)Expression.Lambda(exp2.Body, par);
problem with this is the input paramter gets changed yes ! but the x inside the body of the lambda "x.mesID" is of the old type.
any way to change all the parameters type in the body too or change the input parameter in away it reflect the body too ?
i guess this is a big problem i always have with LINQ , since between layers i cant pass the generated classes , as this will make layers coupled , so i have to make light weight classes , now how do i use a method like _db.Messages.where(); from busiess layer ?!! while busniess layer doesnt know anything about Message type it only know MessageDTO.
No, basically. Expression trees are immutable, and contain full member meta-data (i.e. that
mesID
ismessageDTO.mesID
). To do this, you would have to rebuild the expression tree from scratch (via a visitor), handling every node type you need to support.If the expression tree is basic this should be OK, but if you need to support the entire gamut? a huge PITA (especially in .NET 4, which adds a lot more node-types).
A basic example that does just what is required for the example; you would need to add more node-types for more complex expressions: