1

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.

Community
  • 1
  • 1
Michiel Cornille
  • 2,067
  • 1
  • 19
  • 42
  • maybe this helps: [Combine several similar SELECT-expressions into a single expression](http://stackoverflow.com/questions/6180704/combine-several-similar-select-expressions-into-a-single-expression) – esiprogrammer Dec 02 '16 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. – Michiel Cornille Dec 05 '16 at 12:49
  • Hey @MichielCornille, can you post your code samples please? Thanks! – David Nov 19 '18 at 20:09
  • 1
    It was 2 years ago, I no longer work at the closed-source company I wrote this for. Sorry! – Michiel Cornille Nov 20 '18 at 15:41

0 Answers0