I am wanting to extend my IQueryable to take a List of fieldnames and only return those columns into a dynamic collection. Essentially I want to implement the "Select" with passed in field/column names. The method would look something like this:
private IQueryable<TEnity> SetSelect(IQueryable<TEntity> query, List<string> fieldnames)
{
//Build up the Expression here
query = query.Select(expressionHere);
return query;
}
So if I were querying against an object like:
public Class Dog
{
int Id{get;set;}
string Name{get;set;}
string Color{get;set;}
DateTime Birthdate{get;set;}
}
But I only wanted to get the Name and Birthdate I could extend the IQueryable by calling SetSelect(query, new List<string>{"Name", "Birthdate"});
And would be returned:
[{"Fido", 01-01-2017}, {"Spot", 05-04-1972}]
Has anybody done something similar, and can help me with building that Expression?
Thanks for any clarification.
NOTE: This is a .Net Core application