I am trying to have a select expression that can be incrementally updated depending on what I receive from the input, something like this:
// Init expression
Expression<Func<Order, object>> selectExpression = x => new
{
x.Id
};
if(selectInput.Title){
// something like this
selectExpression = selectExpression.Add(x => new
{
x.Title
});
}
if(selectInput.ClientFullName){
// something like this
selectExpression = selectExpression.Add(x => new
{
ClientFullname = x.Client.Fullname
});
}
// Use expression in a LINQ query (for an EF Core query)
var result = await queryable.Select(selectExpression).ToListAsync();
Then I would expect to have a result like this
{
"Id": 568,
"Title": "Order 567",
"ClientFullname": "John Smith"
}
Is this something that is possible? The only examples I found online are about .Where(). Am I going into the wrong direction?
Thank you!