c# – Automapper – 同时展平列表

c# – Automapper – 同时展平列表,第1张

概述我有一个我从客户端收到的视图模型,看起来像这样 public class OrderViewModel{ public string Name{get;set;} public string ContactDetails {get;set;} public List<FunkyThing_ViewModel> {get;set;}}public class F 我有一个我从客户端收到的视图模型,看起来像这样
public class Orderviewmodel{     public string name{get;set;}     public string ContactDetails {get;set;}     public List<FunkyThing_viewmodel> {get;set;}}public class FunkyThing_viewmodel{    public string Thingname{get;set;}    public string Colour{get;set;}    public string Size{get;set;}}

我希望将其映射到域模型列表,其中每个模型看起来更像这样:

public class Order{   public string name{get;set;}   public string ContactDetails {get;set;}   public string Thingname{get;set;}   public string Colour{get;set;}   public string Size{get;set;}}

所以我想要最终看到这样的东西:

List<Order> orders = new Orders();Mapper.CreateMap<Orderviewmodel,List<Order>>();//Something in here to ensure each funky thing creates an additional order....Mapper.Map(viewmodel,orders);
解决方法
using System.Collections.Generic;using System.linq;using autoMapper;using NUnit.Framework;using SharpTestsEx;namespace StackOverflowExample.automapper{    public class Orderviewmodel    {        public string name { get; set; }        public string ContactDetails { get; set; }        public List<FunkyThingviewmodel> FunkyThingviewmodels { get; set; }    }    public class FunkyThingviewmodel    {        public string Thingname { get; set; }        public string Colour { get; set; }        public string Size { get; set; }    }    public class Order    {        public string name { get; set; }        public string ContactDetails { get; set; }        public string Thingname { get; set; }        public string Colour { get; set; }        public string Size { get; set; }    }    [TestFixture]    public class FlattenWithListTests    {        [Test]        public voID FlattenListtest()        {            //arrange            var source = new Orderviewmodel                {                    name = "name",ContactDetails = "contact",FunkyThingviewmodels = new List<FunkyThingviewmodel>                        {                            new FunkyThingviewmodel {Colour = "red"},new FunkyThingviewmodel {Colour = "blue"}                        }                };            Mapper.CreateMap<FunkyThingviewmodel,Order>();            Mapper.CreateMap<Orderviewmodel,List<Order>>()                  .ConvertUsing(om => om.FunkyThingviewmodels.Select(                      ftvm =>                          {                              var order = Mapper.Map<Order>(om);                              Mapper.Map(ftvm,order);                              return order;                          }).ToList());            //act            var mapped = Mapper.Map<List<Order>>(source);            //assert            mapped[0].Satisfy(m =>                              m.name == source.name &&                              m.ContactDetails == source.ContactDetails &&                              m.Colour == "red");            mapped[1].Satisfy(m =>                              m.name == source.name &&                              m.ContactDetails == source.ContactDetails &&                              m.Colour == "blue");        }    }}
总结

以上是内存溢出为你收集整理的c# – Automapper – 同时展平列表全部内容,希望文章能够帮你解决c# – Automapper – 同时展平列表所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1248334.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存