加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将DTO转移到ViewModel

发布时间:2020-12-16 01:55:22 所属栏目:百科 来源:网络整理
导读:这是我的数据传输对象 public class LoadSourceDetail{ public string LoadSourceCode { get; set; } public string LoadSourceDesc { get; set; } public IEnumerableReportingEntityDetail ReportingEntity { get; set; }}public class ReportingEntityDet
这是我的数据传输对象

public class LoadSourceDetail
{
  public string LoadSourceCode { get; set; }
  public string LoadSourceDesc { get; set; }
  public IEnumerable<ReportingEntityDetail> ReportingEntity { get; set; }
}

public class ReportingEntityDetail
{
  public string ReportingEntityCode { get; set; }
  public string ReportingEntityDesc { get; set; }
}

这是我的ViewModel

public class LoadSourceViewModel
{
    #region Construction

        public LoadSourceViewModel ()
        {
        }

        public LoadSourceViewModel(LoadSourceDetail data)
        {
            if (data != null)
            {
                LoadSourceCode = data.LoadSourceCode;
                LoadSourceDesc = data.LoadSourceDesc;
                ReportingEntity = // <-- ?  not sure how to do this 
            };
        }


    #endregion
    public string LoadSourceCode { get; set; }  
    public string LoadSourceDesc { get; set; }
    public IEnumerable<ReportingEntityViewModel> ReportingEntity { get; set; }  
}

public class ReportingEntityViewModel 
{ 
    public string ReportingEntityCode { get; set; }
    public string ReportingEntityDesc { get; set; } 
}

}

我不确定如何将数据从LoadSourceDetail ReportingEntity传输到LoadSourceViewModel ReportingEntity.我正在尝试将数据从一个IEnumerable传输到另一个IEnumerable.

解决方法

没有AutoMapper,您将不得不逐个映射每个属性,

像这样的东西:

LoadSourceDetail obj = FillLoadSourceDetail ();// fill from source or somewhere

     // check for null before
    ReportingEntity = obj.ReportingEntity
                     .Select(x => new ReportingEntityViewModel() 
                        { 
                           ReportingEntityCode  = x.ReportingEntityCode,ReportingEntityDesc  x.ReportingEntityDesc
                         })
                     .ToList(); // here is  'x' is of type ReportingEntityDetail

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读