自动将 Ienumerable<T> 中的 Key 值自动映射到 flat string[]

Automap value of Key in Ienumerable<T> to flat string[]

我正在接收来自 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 映射到另一个对象中的字符串[]。

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"
  ]


尝试像这样使用 .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)));