I am saving an object in my database, it stores the ID which relates to an object stored in another table. When I retrieve this from the database, I want to return the details of this class to the client along with the name of the related object. ie,
AntifungalAgent
ID: 1
Name: "Drug1"
InteractingDrug
ID: 1
Name: "Drug2"
Interaction
ID: 1
AntifungalID: 1
InteractingDrugID: 1
When I retrieve the data for the Interaction from my controller, the ID
is the ID
of the interactingDrug
so it returns all interactions with that interacting drug.
[HttpGet("{id}")]
public async Task<ActionResult<List<DrugInteractions>>> getInteraction(int id)
{
return await _context.DrugInteractions
.Include(di => di.InteractingDrug)
.Include(di => di.AntifungalAgent)
.Where(di => di.InteractingDrug.ID == id)
.ToListAsync();
}
Currently, I get a list of all the interactions as expected. But I only get a number for the antifungalAgentID
and the InteractingDrugID
.
I want to also get the name of these 2 drugs.
I have created a DrugInteractionDto
class.
public class DrugInteractionDTO
{
public string Antifungal { get; set; }
public string InteractingDrug { get; set; }
public string BasicDetailedInformation { get; set; }
public string ProDetailedInformation { get; set; }
public Severity Severity { get; set; }
public int ID { get; set; }
}
But I am stuck on adding the names of the antifungalAgent
and interactingDrug
to this DTO, so that I can return this to the client and use the names in the UI.
1条答案
按热度按时间kr98yfug1#
根据您提供的数据,
DrugInteraction
是AntifungalAgent
和InteractingDrug
表之间的关联表。抗真菌剂(1..m)--〉药物相互作用(m..1)〈--相互作用药物
生成的
DrugInteraction
实体类应如下所示:您需要
.Select()
将每个对象从DrugInteractions
转换为DrugInteractionDTO
类型。