第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

自動映射數(shù)組類

自動映射數(shù)組類

C#
qq_遁去的一_1 2023-09-24 16:23:25
我有一個目標對象ArrayOfStudents[]含有StudentId,    AddressInfo,    MarksInfo源對象是public class Details{    public Student[] Student;)學生類包含學生號、地址信息、標記信息我想要地圖Student[]和ArrayOfStudents[]我嘗試了以下方法,但沒有成功Map.CreateMap<Student,ArrayOfStudents>() .ReverseMap(); Map.CreateMap<Details.Student,ArrayOfStudents>() .ReverseMap();我應該如何繪制這個案例?它拋出以下未映射錯誤學生號、地址信息、標記信息
查看完整描述

1 回答

?
有只小跳蛙

TA貢獻1824條經(jīng)驗 獲得超8個贊

使用 Automapper,您可以將一種類型映射到另一種類型。當您這樣做時,將自動映射相同類型的數(shù)組。


ArrayOfStudents在您的情況下,您將創(chuàng)建和之間的地圖Student。這將是一個簡單的映射,因為兩個映射類型之間的類型和名稱是相同的:


public class MappingProfile : Profile

{

    public MappingProfile()

    {

        this.CreateMap<Student, ArrayOfStudents>();


        this.CreateMap<ArrayOfStudents, Student>();

    }

}

現(xiàn)在,無論您打算在哪里進行實際映射(例如 RESTful 控制器),您都可以執(zhí)行以下操作:


public class MyController

{

    private readonly IMapper mapper;


    public MyController(IMapper mapper)

    {

        this.mapper = mapper;

    }


    // Then in any of your methods:

    [HttpGet]

    public IActionResult MyMethod()

    {

        var objectsToMap = details.Student; // This is an array of Student type.

        var mappedObjects = this.mapper.Map(objectsToMap); // This will be an array of ArrayOfStudents.

        // do what you will with the mapped objects.

    }

}

想法是,您注冊類型的映射(包括類型中成員的類型)。然后,這些類型的集合的映射由 Automapper 自動處理。


查看完整回答
反對 回復 2023-09-24
  • 1 回答
  • 0 關(guān)注
  • 173 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號