57 lines
1.8 KiB
C#
57 lines
1.8 KiB
C#
using AutoMapper;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using WMS.Web.Core;
|
|
|
|
namespace WMS.Web.Domain.Mappers
|
|
{
|
|
/// <summary>
|
|
/// 集合映射
|
|
/// </summary>
|
|
public static class MapperList
|
|
{
|
|
/// <summary>
|
|
/// 映射实体里 集合属性
|
|
/// </summary>
|
|
/// <typeparam name="ST"></typeparam>
|
|
/// <typeparam name="DT"></typeparam>
|
|
/// <param name="mapper"></param>
|
|
/// <param name="sourcList"></param>
|
|
/// <param name="destinationList"></param>
|
|
/// <returns></returns>
|
|
public static List<DT> ToMapList<DT, ST>(this IMapper mapper, List<ST> sourcList, List<DT> destinationList)
|
|
where ST : EntityBase
|
|
where DT : EntityBase
|
|
{
|
|
for (int i = 0; i < destinationList.Count; i++)
|
|
{
|
|
int id = destinationList[i].Id;
|
|
var detail = sourcList.FirstOrDefault(f => f.Id == id);
|
|
if (detail == null)
|
|
{
|
|
destinationList.Remove(destinationList[i]);
|
|
i--;//在列表移除掉一条数据后 必须把index回拨一位 因为数据在移除一条后会调整下标
|
|
}
|
|
}
|
|
|
|
foreach (var st in sourcList)
|
|
{
|
|
if (st.Id == 0)
|
|
destinationList.Add(mapper.Map<DT>(st));
|
|
else
|
|
{
|
|
var detail = destinationList.FirstOrDefault(f => f.Id == st.Id);
|
|
|
|
if (detail != null)
|
|
mapper.Map(st, detail);
|
|
//如果在目标数据里没找到这条id的数据 则不作处理
|
|
}
|
|
}
|
|
|
|
return destinationList;
|
|
}
|
|
}
|
|
}
|