【发布时间】:2022-12-07 06:23:00
【问题描述】:
我的 c# 控制器中有以下代码,
[HttpGet("antifungal/{name}")]
public List<DrugInteractionDTO> test(string name)
{
var DI = (from D1 in _context.DrugInteractions
join D2 in _context.DrugInteractionReferences
on D1.ID equals D2.DrugInteractionId into joined
from D3 in joined.DefaultIfEmpty()
where D1.AntifungalAgent.Name.ToLower().Contains(name.ToLower())
select new DrugInteractionDTO
{
Severity = D1.Severity,
SeverityAsString = D1.Severity.ToString(),
ProDetailedInformation = D1.ProDetailedInformation,
BasicDetailedInformation = D1.BasicDetailedInformation,
Antifungal = D1.AntifungalAgent.Name,
InteractingDrug = D1.InteractingDrug.GenericName,
ID = D1.ID,
Count = 2
//DrugInteractionReferences
}).ToList();
return DI.OrderBy(x => x.InteractingDrug).ToList();
}
我的 DrugInteractionDTO 模型有一个字段 List<DrugInteractionReferences> 我想添加到其中。
我的两个表是interactions和references,每次交互都可以有很多引用。
我习惯于使用 Java (Spring),我不知道如何返回属于 DTO 中每个交互的引用。我正在帮助的当前应用程序非常旧并且存在很多问题。
是否可以将引用列表添加到在此代码中创建的每个 interactionDTO?
【问题讨论】:
-
是的,但您不能在选择语句中投射实体类型 DrugInteractionReferences。您必须创建一个 DTO 对象并在您的 DrugInteractionDTO 类中使用它。
-
这样做有什么建议吗?我可以用同样的方法做到这一点吗?目前我有交互列表,我想向这些交互中的每一个添加一个引用该交互的引用列表