简体   繁体   English

如何在 C# 中使用 Automapper 将 map 阵列到阵列

[英]How to map array to array using Automapper in C#

I have below Source and Target class.我有以下源和目标 class。

public class Source
    {
        public string BookName { get; set; }
        public Bookstore[] BookStore { get; set; }
    }

    public class Bookstore
    {
        public Address[] Address { get; set; }
    }

    public class Address
    {
        public string Street { get; set; }
    }

Target class as below目标 class 如下

public class Target

    {
        public string Book { get; set; }
        public Store[] Store { get; set; }
    }
    public class Store
    {
        public Geo Geo { get; set; }
    }

    public class Geo
    {
        public Location[] Location { get; set; }
    }

    public class Location
    {
        public string Street { get; set; }
    }

I am looking for solution on proper mapping where source file data gets copied to target fields like below,我正在寻找正确映射的解决方案,其中源文件数据被复制到如下目标字段,

CreateMap<Source, Target>()
                .ForMember(dest => dest.Book, o => o.MapFrom(src => src.BookName));
               
CreateMap<Bookstore, Store>()
                .ForMember(dest => dest.Geo.Location, o => o.MapFrom(src => src.Address));

But I see a few errors like "resolve to top-level member" etc or Array fields, Geo and Location remains empty.但是我看到一些错误,例如“解析为顶级成员”等或数组字段,地理和位置仍然为空。

I would like to map source data to the destination.我想将 map 源数据发送到目的地。

Below is a source file example下面是一个源文件示例

var jsonText = @"                      
{
  "BookName": "Test",
  "BookStore": [
    {
      "Address": [
        {
          "Street": "1234"
        }
      ]
    }
  ]
}";


            var sourType = JsonConvert.DeserializeObject<Source>(jsonText);

The target file expected is as below, where "Geo" object is added,预期的目标文件如下,其中添加了“Geo”object,

{
  "Book": "Test",
  "Store": [
    {
      "Geo": {
        "Location": [
          {
            "Street": "1234"
          }
        ]
      }
    }
  ]
}

You need a custom converter for this type of wrapping.对于这种类型的包装,您需要一个自定义转换器。 Though your JSON result should be like this according to your model:尽管根据您的 model,您的 JSON 结果应该是这样的:

{
  "Book": "Test",
  "Store": [
    {
      "Geo": {
        "Location": [
          {
            "Street": "1234"
          }
        ]
      }
    }
  ]
}

Btw here is the solution:顺便说一句,这是解决方案:

Expression < Func < Bookstore, Store >> storeConverter = p => (new Store {
  Geo = new() {
    Location = p.Address
      .Select(ad => new Location {
        Street = ad.Street
      }).ToArray()
  }
});

CreateMap < Source, Target > ()
  .ForMember(dest => dest.Book, o => o.MapFrom(src => src.BookName))
  .ForMember(dest => dest.Store, o => o.MapFrom(src => src.BookStore));

CreateMap < Bookstore, Store > ()
  .ConvertUsing(storeConverter);

You can move the expression in a separate class type of converter for more cleaner code.您可以在单独的 class 类型的转换器中移动表达式以获得更简洁的代码。 see details in the documentation https://docs.automapper.org/en/stable/Custom-type-converters.html请参阅文档https://docs.automapper.org/en/stable/Custom-type-converters.html中的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM