Search code examples
c#linq.net-coreentity-framework-core

Get the details of a related object from before returning to client


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.


Solution

  • From your provided data, the DrugInteraction is the associate table between AntifungalAgent and InteractingDrug tables.

    AntifungalAgent (1..m) --> DrugInteraction (m..1) <-- InteractingDrug

    The generated DrugInteraction entity class should be looked as below:

    public class DrugInteraction
    {
        public int ID { get; set; }
    
        public int AntifungalAgentID { get; set; }
    
        public int InteractingDrugID { get; set; }
    
        public virtual AntifungalAgent AntifungalAgent { get; set; }
    
        public virtual InteractingDrug InteractingDrug { get; set; }
    }
    

    You need the .Select() to transform the each object from DrugInteractions to DrugInteractionDTO type.

    public async Task<ActionResult<List<DrugInteractionDTO>>> getInteraction(int id)
    {
        return await _context.DrugInteractions
                .Include(di => di.InteractingDrug)
                .Include(di => di.AntifungalAgent)
                .Where(di => di.InteractingDrug.ID == id)
                .Select(x => new DrugInteractionDTO
                {
                    Antifungal = x.AntifungalAgent.Name,
                    InteractingDrug = x.InteractingDrug.Name,
                    ID = x.ID,
                    // Following properties
                })
                .ToListAsync();
    }