简体   繁体   English

映射集合时,将忽略具有Automapper的集合中的项目

[英]Ignoring items in collection with Automapper when mapping collection

I am mapping an collection of items 我正在映射项目集合

var List<A> myCollection = new List<A>();

public class A
{
    bool HasChanges {get;set;}
}

var mappedCollection = Map(myCollection);

then I only want to map the items where HasChanges == true 那么我只想映射HasChanges == true的项目

Is this possible? 这可能吗?

使用Linq:

var mappedCollection = Map(myCollection.Where(x => x.HasChanges == true).ToList());

Automapper has custom type converters: Automapper具有自定义类型转换器:

https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters https://github.com/AutoMapper/AutoMapper/wiki/Custom-type-converters

// this is your converter
public class ATypeConverter : ITypeConverter<string, A>
{
    public A Convert(ResolutionContext context)
    {
          // implement conversion logic
    }
}

// add this in a bootstrapper in your app
Mapper.CreateMap<string, A>().ConvertUsing<ATypeConverter>();

now after you Map your objects it will convert them using your custom mapper, which will allow you to skip the items with change. 现在,在Map对象之后,它将使用自定义映射器将其转换,这将允许您跳过更改项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM