Automapper将对象映射到具有第一个对象的嵌套列表的对象

时间:2017-10-07 23:44:43

标签: c# automapper

我正在上课:

import java.util.ArrayList;

public class AdvDotComTable {
    public boolean createTable(int rows, int columns) {
        if (rows == 26) {
            //When true is returned, the program will let the user know they have to choose a lower number
            return true;
        }
        String[] dotComs = {"Pets.com", "Amazon.com", "Target.com", "Apple.com", "Microsoft.com", "Steampowered.com"};
        ArrayList<Character> row = new ArrayList<Character>();
        ArrayList<Integer> column = new ArrayList<Integer>();
        char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
        int number = 0;
        while (rows != 0 && columns != 0) {
            row.add(letters[number]);
            column.add(number);
            System.out.print(row.get(number) + column.get(number));
            number++;
            rows--;
            columns--;
        }
        return false;
    }
}

我想使用Automapper将其映射到:

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

我该如何映射?我需要自定义解析器吗?

2 个答案:

答案 0 :(得分:3)

假设您要获得需要映射到KempeCollector的Kempe对象列表,您可以使用它来实现:

    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;
        });

    });

运行样本:

    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);

答案 1 :(得分:1)

关于该映射的所有内容都是手动的,这使得它在我看来比linq查询更糟糕(你可以通过编写函数来重用它)。但是如果你想使用AM,至少要尝试利用它可以做的事情。

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;}
}