我想映射List<List<Item>>到,List<List<Item2>>但我不能讓它作業。有沒有辦法使用簡單的配置來做到這一點,或者這種情況是否需要撰寫自定義轉換器?這是我的情況(也可在dotnetfiddle上找到):
using System;
using System.Collections.Generic;
using AutoMapper;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
var configuration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<ItemCollection, List<List<Item2>>>();
cfg.CreateMap<List<List<Item>>, List<List<Item2>>>();
cfg.CreateMap<List<Item>, List<Item2>>();
cfg.CreateMap<Item, Item2>().ForMember(x => x.Name, opt => opt.MapFrom(x => x.Name));
});
var mapper = new Mapper(configuration);
var collection = new ItemCollection();
collection.Add(new List<Item>(){new Item{Name = "item"}});
var dest = mapper.Map<List<List<Item2>>>(collection);
Console.WriteLine(JsonConvert.SerializeObject(dest));
}
}
public class ItemCollection : List<List<Item>>
{
}
public class Item
{
public string Name { get; set; }
}
public class Item2
{
public string Name { get; set; }
}
因此,我想[[{Name = "item"}]]在輸出視窗中看到。
uj5u.com熱心網友回復:
您不需要指定如何映射到集合,只需指定單個型別。
https://dotnetfiddle.net/D6HGik
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/365809.html
下一篇:使用for為字串分配不同的值
