在我的业务逻辑类中,我正在加入两个数据模型并以IEnumerable的形式返回控制器。我需要使用automapper将这些集合映射到List。但它没有按预期工作。
逻辑课程
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()
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;
}
但结果映射并不像预期的那样。
答案 0 :(得分:1)
像这样更改您的代码。
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;
}