I have Project entity and ProjectDTO.
I'm trying to create an WebAPI controller method that can take and return ProjectDTOs and make it support OData.
The problem is that I'm using ORM that can query the database using Project entity not Project DTO. Is there any way that I can apply filtering/sorting/paging from OData based on ProjectDTO to Project entity query?
public ODataQueryResult<ProjectDTO> GetProjects(ODataQueryOptions<ProjectDTO> query)
{
var context = new ORM_Context();
var projects = context.Projects; // IQueryable<Project>
var projectDtos = query.ApplyTo(projectDTOs)); // <-- I want to achieve something similar here
var projectDTOs =
projects.Select(
x =>
new ProjectDTO
{
Id = x.Id,
Name = x.Name
});
var projectsQueriedList = projectDtos.ToList();
var result = new ODataQueryResult<ProjectDTO>(projectsQueriedList, totalCount);
return result;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…