C# 自动映射器:如何将Lazy映射到TTo?

C# 自动映射器:如何将Lazy映射到TTo?,c#,automapper,C#,Automapper,我需要将懒惰列表映射到TTo列表。我所做的不起作用-我只得到空值。没有懒惰的包装,它的工作完美。我做错了什么 // Map this list // It's passed in as an argument and has all the data IList<Lazy<Employee>> employees public static IList<TTo> MapList(IList<Lazy<TFrom>> fromModel

我需要将懒惰列表映射到TTo列表。我所做的不起作用-我只得到空值。没有懒惰的包装,它的工作完美。我做错了什么

// Map this list
// It's passed in as an argument and has all the data
IList<Lazy<Employee>> employees

public static IList<TTo> MapList(IList<Lazy<TFrom>> fromModel)
        {
            Mapper.CreateMap<Lazy<TFrom>, TTo>();// Is this needed?
            return Mapper.Map<IList<Lazy<TFrom>>, IList<TTo>>(fromModel);

            // This doesn't work
            // return Mapper.Map<IList<TTo>>(fromModel.Select(x => x.Value));
        }

// Maps the child classes
Mapper.CreateMap<Lazy<Employee>, EmployeeDTO>()
                .ForMember(x => x.AccidentDTO, i => i.MapFrom(model => model.Value.GetAccident()))
                .ForMember(x => x.CriticalIllnessDTO, i => i.MapFrom(model =>                 model.Value.GetCriticalIllness()))
                .ForMember(x => x.ValidationMessages, i => i.MapFrom(model => model.Value.ValidationMessages));

// Returns nulls !!!
var dataDTO = MyMapper<Lazy<Employee>, EmployeeDTO>.MapList(employees);

以下是我为解决我的问题所做的。如果有人有更好的想法,请让我知道,我会把它标记为答案

// Get the Values of the Lazy<Employee> items and use the result for mapping
var nonLazyEmployees = employees.Select(i => i.Value).ToList();
var dataDTO = MyMapper<Employee, EmployeeDTO>.MapList(nonLazyEmployees);

public static IList<TTo> MapList(IList<Lazy<TFrom>> fromModel)
{
     Mapper.CreateMap<Lazy<TFrom>, TTo>();
     return Mapper.Map<IList<Lazy<TFrom>>, IList<TTo>>(fromModel);
}

// Maps the child classes
Mapper.CreateMap<Employee, EmployeeDTO>()
     .ForMember(x => x.AccidentDTO, i => i.MapFrom(model => model.GetAccident()))
     .ForMember(x => x.CriticalIllnessDTO, i => i.MapFrom(model => model.GetCriticalIllness()))
     .ForMember(x => x.ValidationMessages, i => i.MapFrom(model => model.ValidationMessages));

您是否可以包含设置员工有无懒惰的代码?可能由于延迟了懒散呼叫的时间,您在某些方面失去了作用。如果您能通读所有可怕的格式,这篇博客文章可能会为您提供一些答案@MikeGuthrie…它作为一个参数传入,并拥有所有数据。