小编Але*_*кий的帖子

当指定条件!= null时,Automapper不会正确映射null List成员

当我尝试映射对象的空列表(成员)时出现问题,考虑到我指定:

.ForAllMembers(opts => opts.Condition((src, dest, srcMember) =>
    srcMember != null
));
cfg.AllowNullCollections = true; // didn't help also
Run Code Online (Sandbox Code Playgroud)

代码中的简短示例:

gi.PersonList = new List<Person>();
gi.PersonList.Add(new Person { Num = 1, Name = "John", Surname = "Scott" });
GeneralInfo gi2 = new GeneralInfo();
gi2.Qty = 3;

Mapper.Map<GeneralInfo, GeneralInfo>(gi2, gi);
Run Code Online (Sandbox Code Playgroud)

gi.PersonList.Count = 0,如何解决?

using System;
using System.Collections.Generic;
using AutoMapper;

public class Program
{
   public static void Main(string[] args)
    {
        Mapper.Initialize(cfg =>
        {
            cfg.AllowNullCollections = true;
            cfg.CreateMap<GeneralInfo, GeneralInfo>()
            .ForAllMembers(opts => opts.Condition((src, dest, srcMember) …
Run Code Online (Sandbox Code Playgroud)

c# automapper mapper automapper-6

5
推荐指数
1
解决办法
973
查看次数

标签 统计

automapper ×1

automapper-6 ×1

c# ×1

mapper ×1