Im trying to implement a dynamic dto. First there will be a information manager where you can select the fields you want to return by the WebApi.
Later in the api you send a request to an endpoint with the DTO's Id you want. This method will recive the info, query it in the db and obtain the fields that integrate the DTO(dynamic number of fields):
[Route("api/workers")]
public IEnumerable<object> GetWorkers([FromRoute] string idEnterprise,int idDTO)
{
//lstDummyFields = _context.Dtos.Include("DtoFields").
//Where(n=>n.idDto==idDTO).select(n=>n.DtoFields.name).ToList();
//The property will look like this, the selected dto could have different properties(dynamic) but always call the same as the entity name fields
List<string> lstDummyFields = new List<string>
{
"idWorker",
"first_name",
"last_name",
"birthday",
"adress"
};
dynamic dto = new ExpandoObject() as IDictionary<string,object>;
foreach (var p in lstDummyProperty)
{
dto.Add(p, null);
}
return _context.workers.Where(n=>n.IdEnterprise==idEnterprise).select.(new { ???? }) .ToList();
}
I dont know how to implement the select clause.Could be related to this post but its sligtly different.
https://stackoverflow.com/a/34726946/2642777
I'm close to the answer or im going the wrong way? Could throw some help to solve this please.