簡體   English   中英

如何使用自動映射器 map 復雜類型

[英]How to map Complex type using automapper

具有復雜嵌套映射的 Automapper。 我正在嘗試 map mydestinationArrayField 和 dest1Array,這里將源對象列表復制到 dest1array。

這是我的源和目標課程。

namespace AutomapperDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SourceObject request = new SourceObject()
                {
                    sourceTypeField = "1",
                    SourceObj1Field = new SourceObj1
                    {
                        SourceObj1Id = "1",
                        SourceObjListss = new List<SourceInnerObjList>
                       {

                       new SourceInnerObjList
                       {
                          SourceObjListItem1Id = 1


                       },
                       new SourceInnerObjList
                       {
                           SourceObjListItem1Id = 2

                       }
                   }

                    }
                };
                var mapper = CreateMapper();
                DestinationObject destination = new DestinationObject();
                destination = mapper.Map<DestinationObject>(request);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static IMapper CreateMapper()
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AllowNullDestinationValues = true;
                cfg.CreateMap<SourceObject, DestinationObject>()
               .ForMember(dest => dest.destinationTypeField, o => o.MapFrom(src => src.sourceTypeField))
               .ForMember(dest => dest.destinationObjectArrayField, o => o.MapFrom(src => new destinationObjectArray()
               {
                   mydestinationArrayField = src.SourceObj1Field.SourceObjListss.Select(x => x.SourceObjListItem1Id).FirstOrDefault().ToString(), //this gives error

                   //dest1Array = src.SourceObj1Field.SourceObjListss // here source objectlist to be copied to dest1array

               }));
            });

            return config.CreateMapper();
        }

    }
}


namespace Automapper
{
    public class SourceObject
    {
        public string sourceTypeField;
        public SourceObj1 SourceObj1Field { get; set; }
    }
    public class SourceObj1
    {
        public string SourceObj1Id { get; set; }

        public ICollection<SourceInnerObjList> SourceObjListss { get; set; }
    }

    public class SourceInnerObjList
    {
        public int SourceObjListItem1Id { get; set; }
        public int SourceObjListItem2d { get; set; }

    }
    public class SourceInnerObj2List
    {
        public int? mycount { get; set; }
        public int? yourcount { get; set; }
    }

}


namespace Automapper
{
    public class DestinationObject
    {
        public string destinationTypeField;
        public destinationObjectArray[] destinationObjectArrayField;
    }

    public class destinationObjectArray
    {
        public string mydestinationArrayField;

        public string myField1;

        public destinationInnerObject1Array[] dest1Array;

        public destinationInnerObject2Array[] dest2Array;
    }
    public class destinationInnerObject1Array
    {
        public string destinationInnerObjectItem11;
        public string destinationInnerObjectItem21;
    }
    public class destinationInnerObject2Array
    {
        public string categoryTypeField;

        public string valueField;

        public string NumberField;
    }
}

在執行映射時,我收到“缺少類型 map 配置或不支持的映射”。

無論我如何配置忽略或自定義映射,它似乎都不喜歡這種嵌套。 任何 Automapper 專家都可以告訴我如何使用像這樣的復雜 object 進行映射。

您的第二個 ForMember 似乎不起作用:

.ForMember(dest => dest.destinationObjectArrayField, o => o.MapFrom(src => new destinationObjectArray() //...

因為在您定義 map 以返回destinationObjectArray()而不是destinationObjectArray []!

所以有一個 map 像:

目標對象數組字段 -> 目標對象數組()

並且沒有像 map 這樣的:

目的地對象陣列字段->目的地對象陣列[]

Automapper 正在告訴你這一點。

你應該這樣做:

    cfg.CreateMap<SourceObject, DestinationObject>()
                .ForMember(dest => dest.destinationTypeField, o => o.MapFrom(src => src.sourceTypeField))
                .ForMember(dest => dest.destinationObjectArrayField,
                    o => o.MapFrom(
                        src => src.SourceObj1Field
                            .SourceObjListss
                            .Select(x => new destinationObjectArray
                            {
                                myField1 = $"first_id: {x.SourceObjListItem2d} second_id: {x.SourceObjListItem1Id}"
                            })
                            .ToArray()
                    ));
    });

強烈建議清理和格式化代碼,看起來你只是迷路了。 IDE 可以提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM