using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMS.Web.Core;
namespace WMS.Web.Domain.Mappers
{
///
/// 集合映射
///
public static class MapperList
{
///
/// 映射实体里 集合属性
///
///
///
///
///
///
///
public static List
ToMapList(this IMapper mapper, List sourcList, List 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(st));
else
{
var detail = destinationList.FirstOrDefault(f => f.Id == st.Id);
if (detail != null)
mapper.Map(st, detail);
//如果在目标数据里没找到这条id的数据 则不作处理
}
}
return destinationList;
}
}
}