简体   繁体   English

具有不同实体的转换返回类型

[英]casting return type with different entity

Here is my method and i would like to cast my return - also i am doing this to prevent from changing my different methods i have and wanted to also know will this have any negative affect if a cast my return? 这是我的方法,我想强制退货-我也在这样做是为了防止更改我拥有的不同方法,并且还想知道如果强制退货会对您产生任何负面影响?

public VisitEntry Get(Guid visitEntryId)
    {
        var visitEntry = _visitEntryRepository.Get(visitEntryId);
        VisitEntryDTO dto = AutoMapper.Mapper.Map<VisitEntry, VisitEntryDTO>(visitEntry);
        dto.CasePartyIds =
            _casePartyService.GetAllForHearingEntry(visitEntryId).Select(caseParty => caseParty.CasePartyId).ToList();
        return dto;
    }

The error i get is for: return dto. 我得到的错误是因为:返回dto。 Stating: that I cannot converty ...Entity.VisitEntryDTO to ...Entity.VisitEntry. 说明:我无法将... Entity.VisitEntryDTO转换为... Entity.VisitEntry。

any clarification will be great. 任何澄清都会很棒。 Thanks! 谢谢!

You likely can't directly cast from VisitEntryDTO to VisitEntry . 您可能无法直接从VisitEntryDTOVisitEntry You'll need to Automap back. 您需要自动映射回来。

However, your repository seemingly already returns VisitEntry objects, so why are you mapping to a VisitEntryDTO in the first place? 但是,您的存储库似乎已经返回了VisitEntry对象,那么为什么首先要映射到VisitEntryDTO Why not just do: 为什么不做:

public VisitEntry Get(Guid visitEntryId)
{
    var visitEntry = _visitEntryRepository.Get(visitEntryId);
    visitEntry.CasePartyIds =
        _casePartyService.GetAllForHearingEntry(visitEntryId).Select(caseParty => caseParty.CasePartyId).ToList();
    return visitEntry;
}

Unless I'm missing something about your VisitEntry class. 除非我缺少有关VisitEntry类的信息。

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

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