使用automapper将过滤器应用于集合 [英] Using automapper to apply a filter to a collection

查看:234
本文介绍了使用automapper将过滤器应用于集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含集合的域模型,我想使用AutoMapper将父图和子图映射到视图模型,但是我不希望被软删除的孩子跨过。例如:

  public class Customer {
public EntitySet< Order> {get; set;}
}
public class Order {
public DateTime?我的AutoMapper定义将是

  Mapper.CreateMap< Customer,CustomerViewModel>(); 
Mapper.CreateMap< Order,OrderViewModel>();

我不希望订单在视图模型中具有DeletedDate值。 / p>

AutoMapper可以吗?非常感谢,提前,

Steve。

解决方案

类似的问题,最后类似于下面的方法为我工作:

  Mapper.CreateMap< Customer,CustomerViewModel>()$ $ b $ ForMember(dest => dest.Orders,
opt => opt.MapFrom(src => src.Orders.Where(o =>!o.DeletedDate.HasValue)));

假设您的Customer实体和CustomerViewModel具有名为Orders的集合。 $ b

I have a domain model that contains a collection and I want to use AutoMapper to map the parent and children to the view model but I don't want children that have been "soft" deleted to be taken across. For instance:

public class Customer {
   public EntitySet<Order> {get;set;}
}
public class Order {
   public DateTime? DeletedDate {get;set;}
}

my AutoMapper definition would be

Mapper.CreateMap<Customer, CustomerViewModel>();
Mapper.CreateMap<Order, OrderViewModel>();

and I don't want Orders to be in the view model that have a value for DeletedDate.

Is that possible in AutoMapper? Many thanks in advance,

Steve.

解决方案

I came across similar issue and finally the approach similar to the one below worked for me:

Mapper.CreateMap<Customer, CustomerViewModel>()
    .ForMember(dest => dest.Orders, 
        opt => opt.MapFrom(src => src.Orders.Where(o => !o.DeletedDate.HasValue)));

This assumes your Customer entity and CustomerViewModel dto have collections named "Orders".

这篇关于使用automapper将过滤器应用于集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆