如何使用AutoMapper映射带有集合字段的类

问题描述

我有Order和OrderDTO类。我要映射它们。

public class OrderDTO
    {
        public int OrderId { get; set; }
        public OrderStatusDTO StatusDto { get; set; }
        public int UserId { get; set; }
        public ICollection<ProductDTO> Products { get; set; }
        public DateTime Time { get; set; }
    }

 public class Order
    {
        public int OrderId { get; set; }
        public OrderStatus Status { get; set; }
        public int UserId { get; set; }
        public ICollection<Product> Products { get; set; }
        public DateTime Time { get; set; }

    }

我使用了这段代码,但是它返回了空列表。

 var config = new MapperConfiguration(cnfg => cnfg.CreateMap<List<Order>,List<OrderDTO>>());
 var map = new Mapper(config);
 return map.Map<List<OrderDTO>>(Db.Orders.FindAll(order => order.UserId == userId).ToList());

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)