0

I am having a class:

public class Kempe
{
   public string Name {get; set;}
   public DateTime DateTaken {get; set;}
   public decimal Score {get; set;}
}

I would like to use Automapper to map it into:

public class KempeCollector
{
   public string Name {get; set;}
   public List<KempeList> Collection {get; set;}
}

public class KempeList 
{
   public DateTime DateTaken {get; set;}
   public decimal Score {get; set;}
}

How do I map this? Do I need custom resolver?

2
  • It depends on what you are trying to achieve. Can you provide us with an example? I guess that you want to map Kempe to KempeList... but mapping Kempe.Name to KempeCollector.Name doesn't feel correct. What if you have two instances of Kempe with different name? What do you expect to have in KempeCollector? Commented Oct 7, 2017 at 23:57
  • I want reverse situation of this stackoverflow.com/questions/18096034/… Commented Oct 8, 2017 at 0:10

2 Answers 2

4

Assuming that you are going to have list of Kempe objects that needs to be mapped to KempeCollector you can make it with this:

    Mapper.Initialize(cfg =>
    {
        cfg.CreateMap<Kempe, KempeList>();
        cfg.CreateMap<List<Kempe>, KempeCollector>().ConvertUsing((kempeList, kempeCollector) =>
        {
            kempeCollector = new KempeCollector
            {
                Name = kempeList[0].Name,
                Collection = new List<KempeList>()
            };

            foreach (var kempe in kempeList)
            {
                kempeCollector.Collection.Add(Mapper.Map<KempeList>(kempe));
            }

            return kempeCollector;
        });

    });

Running sample:

    List<Kempe> kList = new List<Kempe>()
    {
        new Kempe{ Name = "1000", DateTaken = DateTime.Today, Score = 1 },
        new Kempe{ Name = "1000", DateTaken = DateTime.Today, Score = 2 }
    }

    var kColl = Mapper.Map<KempeCollector>(kList);
Sign up to request clarification or add additional context in comments.

4 Comments

that's exactly what i did, i just wrote custom resolver, by confirming we think the same, here accepted answer for you. sretno momak
AM only gets in the way, this would be easier to do with a LINQ statement. You're mapping by hand anyway.
@LucianBargaoanu You are right to some point but if he has Automapper already in place then doing automapper way feels natural and if he is going to map Kempe to KempeCollector multiple times on multiple places it will be also easier/natural to use Automapper. If he is using it for one time only then it would be ok to use LINQ.
I used automapper for mapping in custom resolver, no manual mapping.
1

Everything is manual about that mapping, and that makes it, in my view, worse than a linq query (which you can reuse by writing a function). But if you want to use AM, at least try to take advantage of what it can do.

static void Main()
{
    Mapper.Initialize(cfg =>
    {
        cfg.CreateMap<Kempe, KempeList>();
        cfg.CreateMap<List<Kempe>, KempeCollector>()
            .ForMember(d=>d.Collection, o=>o.MapFrom(s=>s));
    });
    Mapper.AssertConfigurationIsValid();    
    var kList = new List<Kempe>{new Kempe{ Name = "1000", DateTaken = DateTime.Today, Score = 1 }, new Kempe{ Name = "3000", DateTaken = DateTime.Today.AddDays(2), Score = 2 }};
    var kColl = Mapper.Map<KempeCollector>(kList).Dump();
}

public class Kempe
{
   public string Name {get; set;}
   public DateTime DateTaken {get; set;}
   public decimal Score {get; set;}
}

public class KempeCollector
{
   public string FirstName {get; set;}
   public List<KempeList> Collection {get; set;}
}

public class KempeList 
{
   public DateTime DateTaken {get; set;}
   public decimal Score {get; set;}
}

2 Comments

You are right about this. I will upvote your answer. Maybe you are missing maping of Name, it should be something like: .ForMember(d => d.Name, o => o.MapFrom(s => s.FirstOrDefault().Name)); @amel-salibasic this solution is definitely much better one, please accept it as the correct answer.
If you can rename it to FirstName, as I did above, it will be mapped by default (it would be FirstOrDefaultName for your mapping). Of course, if you have to keep the existing name, you need the MapFrom.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.