[英]Bind Dto with model and just return one Dto
首先看看我的代碼:這是ConceptDto
:
public Guid ConceptId { get; set; }
public string ConceptName { get; set; }
public string ConceptLatinName { get; set; }
public List<ConceptSubDto> ConceptSubSidiary { get; set;}
這是ConceptSubDto
:
public Guid ConceptSubId { get; set; }
public string ConceptSubName { get; set; }
我有這樣的域名。
現在這是我的應用層,它有邏輯我想通過 id 獲取這些並只返回一個ConceptDto
但我不知道必須 map 這些 dto 與域模型:
public async Task<ConceptManagementDto> GetConceptById(Guid id)
{
var concept = await _conceptManagementRepository.Query()
.Include(x => x.conceptSubSidiaries)
.GetOneAsync(x => x.Id == id);
return new ConceptManagementDto
{
ConceptManagementId = concept.Id,
ConceptName = concept.ConceptName,
ConceptLatinName = concept.ConceptLatinName,
ConceptSubSidiary = ??
};
}
如果您的 DTO 的字段較少,則此查詢應該有效:
public async Task<ConceptManagementDto> GetConceptById(Guid id)
{
return await _conceptManagementRepository.Query()
.Where(x => x.Id == id)
.Select(x = new ConceptManagementDto
{
ConceptManagementId = x.Id,
ConceptName = x.ConceptName,
ConceptLatinName = x.ConceptLatinName,
ConceptSubSidiary = x.ConceptSubSidiaries
.Select(sub => new ConceptSubDto
{
ConceptSubId = sub.ConceptSubId,
ConceptSubName = sub.ConceptSubName
})
.ToList()
}).First();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.