2

Given an input of two expressions, e.g.:

Expression<Func<Customer,string>> nameExpression = x=>x.Name;
Expression<Func<Customer,string>> nameExpression = x=>x.MarketSegment.Name;

and a

IQueryable<Customer> query = ..//fetch from dbContext;

I want to dynamically create an expression that selects these properties from the query.

the end result would have to execute as follows:

Expression<IQueryable<Customer>,IQueryable<dynamic>> query = query.Select(x=>new{
  x=>x.Name,
  x=>x.MarketSegment.Name
});

I figured out that Expression.New could be an option in this question, but I'm unable to figure out how to pass expressions to it.

4
  • maybe this helps: Combine several similar SELECT-expressions into a single expression Commented Dec 2, 2016 at 13:53
  • Thanks, I managed to get it working using an expression visitor to bind x from the query select to the x parameter in the body of the expressions passed as an input. I'll try to post an in-depth answer with some code samples later on, when we cleaned it all up. Commented Dec 5, 2016 at 12:49
  • 1
    Hey @MichielCornille, can you post your code samples please? Thanks! Commented Nov 19, 2018 at 20:09
  • 1
    It was 2 years ago, I no longer work at the closed-source company I wrote this for. Sorry! Commented Nov 20, 2018 at 15:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.