Non voglio usare. Includere per ottenere le tabelle dell'intero bambino. Ho solo bisogno di selezionare colonne.
public class ProjectTypeDTO {
public string Type { get; set; }
}
public class CourseDTO {
public string CourseCode { get; set; }
public string CourseTitle { get; set; }
}
public class ProjectDTO {
public int Id { get; set; }
public ProjectTypeDTO ProjectType { get; set; }
public CourseDTO Course { get; set; }
public string StartTerm { get; set; }
public DateTime SignOff { get; set; }
public DateTime StartDateTime { get; set; }
}
[HttpGet("getallprojects")]
public IActionResult GetAllProjects()
{
var projects = _context.Projects
.Select(p => new ProjectDTO
{
Id = p.Id,
ProjectType = { Type = p.ProjectType.Type },
Course = { CourseCode = p.Course.CourseCode, CourseTitle = p.Course.CourseTitle },
StartTerm = p.StartTerm,
SignOff = p.SignOff,
StartDateTime = p.StartDateTime,
}).ToList();
return Ok(projects);
}
Questo sta lanciando un "NotImplementedException: il metodo o l'operazione non è implementata". errore.
L'ho provato come funzione anonima e funziona.
var projects = _context.Projects
.Select(p => new
{
p.Id,
p.ProjectType.Type,
p.SignOff,
p.StartDateTime,
p.Course.CourseCode,
p.Course.CourseTitle,
p.StartTerm
}).ToList();
Un tipo anonimo non funzionerà per la mia app, dal momento che ho bisogno di essere in grado di apportare modifiche a questi dati prima che venga restituito.
Sulla base di altri esempi che ho visto qui e altri siti, questo sembra corretto. Potrebbe essere un bug?
Non ho visto prima quella sintassi per i sottooggetti. per esempio:
ProjectType = { Type = p.ProjectType.Type }
Credo che dovrebbe essere:
ProjectType = new ProjectTypeDTO{ Type = p.ProjectType.Type }