2010-08-02 131 views
3

我有一个类FOOS:是否可以将对象映射到Automapper中的List?

public class Foos 
{ 
    public string TypeName; 

    public IEnumerable<int> IDs; 
} 

是否有可能将其与AutoMapper映射到富的对象的IList?

public class Foo 
{ 
    public string TypeName; 

    public int ID; 
} 
+3

:)它很可能与http://valueinjecter.codeplex.com/,这其实事情之一,我使用它为 – Omu 2010-08-02 22:24:20

+0

@Omu我从来没有见过这个项目......谢谢! – stacker 2010-08-03 04:55:13

回答

4

Omu的回答让我想到了一个想法如何解决问题(建议为+1)。我用ConstructUsing()方法和它的工作对我来说:

private class MyProfile : Profile 
    { 
     protected override void Configure() 
     { 
      CreateMap<Foos, Foo>() 
       .ForMember(dest => dest.ID, opt => opt.Ignore()); 
      CreateMap<Foos, IList<Foo>>() 
       .ConstructUsing(x => x.IDs.Select(y => CreateFoo(x, y)).ToList());     
     } 

     private Foo CreateFoo(Foos foos, int id) 
     { 
      var foo = Mapper.Map<Foos, Foo>(foos); 
      foo.ID = id; 
      return foo; 
     } 
    } 
+0

对你有好处,我的看起来更简单:) – Omu 2010-08-04 14:11:41

+0

那么你为什么不发表一个答案,而不是暗示在评论? – xr280xr 2017-10-24 20:06:35

相关问题