简体   繁体   English

添加对象列表到实体框架中创建的 DTO c#

[英]Add a list of objects to DTO create in entity framework c#

I have the following code in my c# controller,我的 c# controller 中有以下代码,

[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();
}

My DrugInteractionDTO model has a field List<DrugInteractionReferences> which I want to add to.我的DrugInteractionDTO model 有一个我想添加到的字段List<DrugInteractionReferences>

my two tables are interactions and references each interaction can have many references.我的两个表是interactionsreferences每个交互可以有很多引用。

I'm used to using Java (Spring) and I can't figure out how to return the references that belong to each interaction in the DTO.我习惯于使用 Java (Spring),我不知道如何返回属于 DTO 中每个交互的引用。 The current application I am helping on is very old and has many many problems.我正在帮助的当前应用程序非常旧并且存在很多问题。

Is it possible to add the list of references to each interactionDTO which is being created in this code?是否可以将引用列表添加到在此代码中创建的每个 interactionDTO?

It is called Eager Loading query, detail entities should be retrieved not by join but subquery expression:它被称为 Eager Loading 查询,详细实体应该不是通过join而是通过子查询表达式来检索:

var DI = (from D1 in _context.DrugInteractions
          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 = _context.DrugInteractionReferences
                  .Where(D2 => D2.DrugInteractionId == D1.ID)
                  .ToList()
          }).ToList();

If you encounter performance issues, add .AsSplitQuery() before last .ToList()如果遇到性能问题,请在最后一个 .ToList( .AsSplitQuery() .ToList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM