亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

在 C# 中將一個對象轉換為另一個對象

在 C# 中將一個對象轉換為另一個對象

C#
慕桂英3389331 2022-11-13 14:18:10
我是 C# 的新手,dotnet 核心。我正在從數據庫中獲取數據,如下所示。這個數據很平淡。我需要轉換為另一個對象。public class Lead    {        public long Id { get; set; }        public short LeadCreatorId { get; set; }        public short LeadOwnerId { get; set; }        public string ProductId { get; set; }        public LeadPriority Priority { get; set; }        public LeadStatus Status { get; set; }        public long LeadCustomerId { get; set; }          public string FirstName { get; set; }        public string LastName { get; set; }        public string EmailAddress { get; set; }        public string MobileNo { get; set; }        public DateTime CreatedOn { get; set; }    }我需要將其轉換為以下對象。public class LeadDto    {        public long Id { get; set; }        public short LeadCreatorId { get; set; }        public short LeadOwnerId { get; set; }        public string ProductId { get; set; }        public LeadPriority Priority { get; set; }        public LeadStatus Status { get; set; }        public LeadCustomer LeadCustomer { get; set; }        public DateTime CreatedOn { get; set; }    }LeadCustomer 如下所示 -public class LeadCustomer{            public long Id { get; set; }            public string FirstName { get; set; }            public string LastName { get; set; }            public string EmailAddress { get; set; }            public string MobileNo { get; set; }}我怎樣才能輕松做到這一點?我可以使用 dto 進行轉換嗎?
查看完整描述

4 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

它們是互聯網上的許多映射器庫。僅舉幾例

  • 自動映射器

  • 映射器

  • 發射映射器

他們都有自己的優點和缺點。我個人覺得編寫自己的映射器方法是值得的,因為您將完全控制自己編寫的內容。

我同意這可能需要一些時間,但不會花這么長時間。


查看完整回答
反對 回復 2022-11-13
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

由于LeadDto并LeadCustomer取決于Lead:


一種選擇是添加一個構造函數,并將LeadDto對象作為參數并映射到屬性。LeadCustomerLead


另一種選擇是創建靜態擴展方法。


public static LeadDto ToLeadDto(this Lead lead)

{

   return new LeadDto(){

      this.Id = lead.Id;

      // Etc..

   }

}

然后你可以使用像


LeadDto myObj = someLead.ToLeadDto();

另一種選擇是使用諸如 AutoMapper 之類的庫。我從來沒有使用過它,并且對于您聲明的需求來說太過分了,IMO。


查看完整回答
反對 回復 2022-11-13
?
躍然一笑

TA貢獻1826條經驗 獲得超6個贊

一個簡單直接的方法可能是在 Lead 類上有一個函數來返回您需要的對象。


public class Lead

{

    //// your properties

    public LeadDto GetOtherObject()

    {

        LeadDto object = new LeadDto();

        ////Map properties from this to object

        return object;

    }

}

否則,如果上述方法在給定的上下文中似乎不正確,則您可以使用靜態實用程序功能。


public static class Utility

{

    public static LeadDto GetOtherObject(Lead leadObject)

    {

        LeadDto object = new LeadDto();

        ////Map properties from leadObject to object

        return object;

    }

}

如果 的耦合LeadDto嚴格只與 Lead 耦合,則可以Lead在 的構造函數中有一個對象作為參數本身LeadDto。


在將屬性從 Lead 映射到 LeadDto 時,請確保您考慮LeadPriority并LeadStatus謹慎,因為它們可能會根據它們是結構還是類而以不同方式工作。


查看完整回答
反對 回復 2022-11-13
?
幕布斯7119047

TA貢獻1794條經驗 獲得超8個贊

正如@G_S 所建議的那樣,我使用了如下所示的自動映射器 -


        CreateMap<Lead, LeadDto>()

            .ForMember(desc => desc.LeadCustomer, opt => opt.ResolveUsing(src =>

            {

                return new LeadCustomer()

                {

                    Id = src.LeadCustomerId,

                    FirstName = src.FirstName,

                    LastName = src.LastName,

                    EmailAddress = src.EmailAddress,

                    MobileNo = src.MobileNo

                };

            }));


查看完整回答
反對 回復 2022-11-13
  • 4 回答
  • 0 關注
  • 463 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號