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之集合和数组映射的更多相关文章

  1. Scala学习——数组/映射/元组

    [<快学Scala>笔记] 数组 / 映射 / 元组 一.数组 1.定长数组 声明数组的两种形式: 声明指定长度的数组 val 数组名= new Array[类型](数组长度) 提供数组初 ...

  2. MyBatis传入集合 list 数组 map参数的写法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...

  3. [Swift]多维数组的表示和存储:N维数组映射到一维数组(一一对应)!

    数组:有序的元素序列. 若将有限个类型相同的变量的集合命名,那么这个名称为数组名.组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量.用于区分数组的各个元素的数字编号称为下标.数组 ...

  4. 获取单列集合,双列集合,数组的Stream流对象以及简单操作

    获取流对象 获取单列集合,双列集合,数组的流对象 单列集合获取流对象: 1.java.util.Collection接口中加入了default方法stream()获取流对象,因此其所有实现类均可通过此 ...

  5. JAVASE(十四) 集合: 数组和集合、Collection、Iterator、List、Set、Map

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 1.数组和集合 1.1 内存中对数据进行存储和管理的“容器”:数组,集合 1.2 数组存储的特点和缺点 ...

  6. 【Java学习笔记】集合转数组---toArray()

    package p2; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ...

  7. C#对多个集合和数组的操作(合并,去重,判断)

    在开发过程中.数组和集合的处理是最让我们担心.一般会用for or foreach 来处理一些操作.这里介绍一些常用的集合跟数组的操作函数.  首先举例2个集合A,B. List<int> ...

  8. 创建泛类集合List以及数组转集合,集合转数组的应用

    List<int> list = new List<int>(); list.Add(); list.Add(); list.Add(); list.AddRange(, , ...

  9. java 中集合和数组互相转换

    package test; import java.util.Arrays;import java.util.List; /** * Created by Administrator on 2016/ ...

随机推荐

  1. 记Asp.Net Core Swagger 使用 并带域接口处理

    引用作者原话:Asp.Net的WebApi中使用Swagger作为说明和测试的页面是非常不错的,比起WebApiTestClient来至少在界面上的很大的提升.但是使用Swagger时如果只是一般的控 ...

  2. window.open新打开窗口与新开标签页

    最近在使用window.open时忽略了一个细节问题:window.open新打开一个窗口,但是有时却是新打开一个窗口有时打开一个新标签页.虽然对一般的需求来说,这个两种情况都无所谓,但是对于那种有强 ...

  3. Python之分支结构

    if lengeh >= 100: #每个条件后面要使用冒号 if lengeh>=10000: ") elif lengeh>=1000: ') else: pass e ...

  4. mongodb初始化并使用node.js实现mongodb操作封装

    mongodb的下载只要在https://www.mongodb.com/网站就能够下载 下载后安装只用一直点next就可以,注意最好使用默认路径安装到C盘,然后在任意位置建立一个文件夹用于储存你的数 ...

  5. linux服务器的相关信息查看(端口占用,cpu、内存占用,防火墙,系统信息,vim编辑器使用等)

    一.端口占用情况   https://www.cnblogs.com/CEO-H/p/7794306.html (1)查看所有端口.进程的使用情况:netstat -tunlp (2)查看某一端口的使 ...

  6. postgresql和redis

    redis 和postgresql区别以及其优缺点 一刹那者为一念,二十念为一瞬,二十瞬为一弹指,二十弹指为一罗预,二十罗预为一须臾,一日一夜有三十须臾. 那么,经过周密的计算,一瞬间为0.36 秒, ...

  7. vue教程1-02 data里面存储数据

    vue教程1-02 data里面存储数据 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  8. 通过XMLHttpRequest和jQuery两种方式实现ajax

    一.XMLHttpRequest实现获取数据 不使用jQuery实现页面不刷新获取内容的方式,我们这里采用XMLHttpRequest原生代码实现:js代码如下: //1.获取a节点,并为其添加Onc ...

  9. MapReduce中的倒排索引

    0.倒排索引资料: http://blog.csdn.net/pzasdq/article/details/51442856 1.三个日志源文件: a.txt hello tom hello jerr ...

  10. 使用安装 php-memcache-client

    1.memcache:是一个高效的分布式内存对象缓存系统 2.  IES---请求--->服务器(apace) | | |---->会查看memcache.是否有IES想要的内容--> ...