使用 Automapper 将加入的域模型映射到视图模型
Mapping joined Domain Model to View Model using Automapper
在我的业务逻辑中 class 我加入了两个数据模型并作为 IEnumerable 返回到控制器。我需要使用 automapper 将这些集合映射到列表。但它没有按预期工作。
逻辑class
public IEnumerable<object> GetPurchaseOrderDetailsByPersonId(long personId)
{
var purchaseOrderDetails = from pom in _unitOfWork.DbSet<PurchaseOrderMain>()
join rep in _unitOfWork.DbSet<RepresentativeMaster>() on pom.REPM_ID equals rep.REPM_ID
where pom.REPM_ID == personId
select new { pom.RM_ID,pom.OrderNo,pom.OrderAmount,pom.OrderDate ,rep.RepName };
return purchaseOrderDetails;
}
控制器
public ActionResult Index()
{
List<object> purchaseOrder = _CLS_PurchaseOrder_BLL.GetPurchaseOrderDetailsByPersonId(PersonId).ToList();
return View(purchaseOrder.ToEntity<OMOS.Models.PurchaseOrderDetails>());
}
ToEntity() 扩展 class
public static List<TDestination> ToEntity<TDestination>(this List<object> OBJSource)
{
AutoMapper.Mapper.CreateMap<object, TDestination>();
List<TDestination> destination = new List<TDestination>();//Handling the null destination
foreach (object source in OBJSource)
{
destination.Add(AutoMapper.Mapper.Map<object, TDestination>(source));
}
return destination;
}
但映射结果与预期不符。
像这样更改您的代码。
public static List<TDestination> ToEntity<TDestination>(this List<object> OBJSource)
{
List<TDestination> destination = new List<TDestination>();//Handling the null destination
foreach (object source in OBJSource)
destination.Add(AutoMapper.Mapper.DynamicMap<TDestination>(source));
return destination;
}
在我的业务逻辑中 class 我加入了两个数据模型并作为 IEnumerable 返回到控制器。我需要使用 automapper 将这些集合映射到列表。但它没有按预期工作。
逻辑class
public IEnumerable<object> GetPurchaseOrderDetailsByPersonId(long personId)
{
var purchaseOrderDetails = from pom in _unitOfWork.DbSet<PurchaseOrderMain>()
join rep in _unitOfWork.DbSet<RepresentativeMaster>() on pom.REPM_ID equals rep.REPM_ID
where pom.REPM_ID == personId
select new { pom.RM_ID,pom.OrderNo,pom.OrderAmount,pom.OrderDate ,rep.RepName };
return purchaseOrderDetails;
}
控制器
public ActionResult Index()
{
List<object> purchaseOrder = _CLS_PurchaseOrder_BLL.GetPurchaseOrderDetailsByPersonId(PersonId).ToList();
return View(purchaseOrder.ToEntity<OMOS.Models.PurchaseOrderDetails>());
}
ToEntity() 扩展 class
public static List<TDestination> ToEntity<TDestination>(this List<object> OBJSource)
{
AutoMapper.Mapper.CreateMap<object, TDestination>();
List<TDestination> destination = new List<TDestination>();//Handling the null destination
foreach (object source in OBJSource)
{
destination.Add(AutoMapper.Mapper.Map<object, TDestination>(source));
}
return destination;
}
但映射结果与预期不符。
像这样更改您的代码。
public static List<TDestination> ToEntity<TDestination>(this List<object> OBJSource)
{
List<TDestination> destination = new List<TDestination>();//Handling the null destination
foreach (object source in OBJSource)
destination.Add(AutoMapper.Mapper.DynamicMap<TDestination>(source));
return destination;
}