AutoMapper之集合和数组映射
9.集合和数组映射
在项目中,集合和数组使用的很多的,继续下来就讲讲他们的映射,很简单。
/// <summary>
/// 源对象
/// </summary>
public class Source
{
public int Value { get; set; }
public string Text { get; set; }
}
/// <summary>
/// 目标对象
/// </summary>
public class Destination
{
public int Value { get; set; }
public string Text { get; set; }
}
/// <summary>
/// 集合和数组映射测试类
/// </summary>
[TestClass]
public class ListAndArrayMaping
{
[TestMethod]
public void ListMapingTest()
{
//初始化映射 和单个对象的映射一样
Mapper.Initialize(cfg => cfg.CreateMap<Source, Destination>());
var srcList = new List<Source> {
new Source { Value = 5,Text="Five" }
};
//在这里指定类型参数,拿第一个为例;源类型:List<Source>;目标类型:IEnumerable<Destination>;
// List映射到IEnumerable;
IEnumerable<Destination> ienumerableDest1 = Mapper.Map<List<Source>, IEnumerable<Destination>>(srcList);
// List映射到ICollection;
ICollection<Destination> icollectionDest1 = Mapper.Map<List<Source>, ICollection<Destination>>(srcList);
// List映射到IList;
IList<Destination> ilistDest1 = Mapper.Map<List<Source>, IList<Destination>>(srcList);
// List映射到List;
List<Destination> listDest1 = Mapper.Map<List<Source>, List<Destination>>(srcList);
// List映射到Array;
Destination[] arrayDest1 = Mapper.Map<List<Source>, Destination[]>(srcList);
//验证List映射到IEnumerable的结果
foreach (var m in ienumerableDest1)
{
Assert.AreEqual("Five", m.Text);//通过
Assert.AreEqual(5, m.Value); //通过
}
//验证List映射到List结果
foreach (var m in listDest1)
{
Assert.AreEqual("Five", m.Text); //通过
Assert.AreEqual(5, m.Value); //通过
}
}
}
AutoMapper还支持以下集合类型的映射:
- IEnumerable
- IEnumerable
- ICollection
- ICollection
- IList
- IList
- List
- Arrays
以后在项目中使用起来就更加方便了!!!
AutoMapper之集合和数组映射的更多相关文章
- Scala学习——数组/映射/元组
[<快学Scala>笔记] 数组 / 映射 / 元组 一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初 ...
- MyBatis传入集合 list 数组 map参数的写法
foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...
- [Swift]多维数组的表示和存储:N维数组映射到一维数组(一一对应)!
数组:有序的元素序列. 若将有限个类型相同的变量的集合命名,那么这个名称为数组名.组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量.用于区分数组的各个元素的数字编号称为下标.数组 ...
- 获取单列集合,双列集合,数组的Stream流对象以及简单操作
获取流对象 获取单列集合,双列集合,数组的流对象 单列集合获取流对象: 1.java.util.Collection接口中加入了default方法stream()获取流对象,因此其所有实现类均可通过此 ...
- JAVASE(十四) 集合: 数组和集合、Collection、Iterator、List、Set、Map
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 1.数组和集合 1.1 内存中对数据进行存储和管理的“容器”:数组,集合 1.2 数组存储的特点和缺点 ...
- 【Java学习笔记】集合转数组---toArray()
package p2; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ...
- C#对多个集合和数组的操作(合并,去重,判断)
在开发过程中.数组和集合的处理是最让我们担心.一般会用for or foreach 来处理一些操作.这里介绍一些常用的集合跟数组的操作函数. 首先举例2个集合A,B. List<int> ...
- 创建泛类集合List以及数组转集合,集合转数组的应用
List<int> list = new List<int>(); list.Add(); list.Add(); list.Add(); list.AddRange(, , ...
- java 中集合和数组互相转换
package test; import java.util.Arrays;import java.util.List; /** * Created by Administrator on 2016/ ...
随机推荐
- unidbgrid单元格操作
unidbgrid单元格操作 //GRID里回车替换TABfunction cellkeydown(sender, td, cellIndex, record, tr, rowIndex, e, eO ...
- 使用PerfView监测.NET程序性能(一):Event Trace for Windows
前言: 在日常项目开发中,我们时不时会遇到程序占用了很高CPU的情况,可能是程序里某些未经优化的代码或者Bug,或者是程序运行压力太大.无论是什么原因,我们总希望能看到到底是哪个方法占用了如此高的CP ...
- Word 如何实现表格快速一分为二
将光标定位在分断处,按快捷键“Ctrl+Shift+Enter“
- StringBuilder 详解 (String系列之2)
本章介绍StringBuilder以及它的API的详细使用方法. 转载请注明出处:http://www.cnblogs.com/skywang12345/p/string02.html StringB ...
- DotNetCore依赖注入实现批量注入
文章转载自平娃子(QQ:273206491):http://os.pingwazi.cn/resource/batchinjectservice 一.依赖注入 通过依赖注入,可以实现接口与实现类的松耦 ...
- WPF放大镜效果
在做WPF项目中,不止两个项目需要有放大镜功能. 第一个项目是一个手术室的远程示教系统,主要是为了方便专家演示病症时,可以放大图片上的某些部位. 第二个项目是一个工厂的MES项目,其中有个功能是质量预 ...
- 文本框仅可接收decimal
文本框html如下: <div><label class="label">价格:</label><input id="TextP ...
- AEAI WM v1.6.0 升级说明,开源工作管理系统
1 升级说明 AEAI WM v1.6.0版是AEAI WM v1.5.0版工作管理系统的升级版本,本次升级的系统是基于AEAI DP 3.8.0_20170228进行打包部署的,对产品中的功能及BU ...
- 【五校联考3day2】C
題意: 現有一平面直角坐標系,有n個點,每一個點必須向某一個方向發射射線,且任意一條射線必須與某一條坐標軸平行.定義一種發射射線的方案是合法的,則方案必須滿足: 1.沒有一條射線交叉 2.沒有一條射線 ...
- Spring中使用StandardServletMultipartResolver进行文件上传
从Spring3.1开始,Spring提供了两个MultipartResolver的实现用于处理multipart请求,分别是:CommonsMultipartResolver和StandardSer ...