Key的自动化值在Ienumerable中 扁平线[]

问题描述 投票:0回答:1

我正在接收我需要压缩的API调用的数据。数据存储在另一个对象内的Ienumerable中。以下是我的传入数据格式的示例。

class Incoming 
{
   public string Something { get; set; }
   public string SomethingElse { get; set; }
   public IEnumerable<Service> Service1 { get; set; }
   public IEnumerable<Service> Service2 { get; set; }
}
class Service
{
   public string One { get; set; }
   public string Two { get; set; }
   public string ServiceName { get; set}
}

我需要将serviceNames映射到另一个对象中的string []。

class outgoing
{
   public string Something { get; set; }
   public string SomethingElse { get; set; }
   public string[] Service1 { get; set; }
   public string[] Service2 { get; set }
}

因此,如果我的传入数据的Service1的值为

{ 
Something: "A", 
SomethingElse "B", 
Service1: [
{ One: one, Two: two, ServiceName: "NameOne" },
{ One: one, Two: two, ServiceName: "NameTwo" },
{ One: one, Two: two, ServiceName: "NameThree" } 
]

我希望回复看起来像:

Something: "A",
SomethingElse: "B",
Service1: {"NameOne", "NameTwo", "NameThree"}

我尝试过使用Construct Using

.ConstructUsing(
   x => new string[] { x.ServiceName}
);

But the results show an array of types rather then values
  "Service1": [
     "Service",
     "Service",
     "Service"
  ]


arrays .net automapper ienumerable
1个回答
0
投票

尝试像这样使用.MapFrom()

 CreateMap<Incoming, outgoing>()
            .ForMember(dest => dest.Service1,
                        opt => opt.MapFrom(
                               src => src.Service1.Select(
                                             service => service.ServiceName)
                        )
            );

奖励(回答评论中的问题,不确定它是否有效):

CreateMap<IEnumerable<StService>, string[]>()
                .ForMember(dest => dest,
                           opt => opt.MapFrom(src => src.Select(service => service.ServiceName)));
© www.soinside.com 2019 - 2024. All rights reserved.