简体   繁体   中英

Automapper from list of object

I have View Model and Class that I have to map. This is my maping setting:

Mapper.CreateMap<ViewModels.objTest, clsTest>().ReverseMap();

I have to map List of View model to my clsTest .

for now I'm using looping like this:

List<clsTest> objListResult = new List<clsTest>();
if (List<objTest> != null)
{
    foreach (var item in objTest)
    {
        objListResult.Add(Mapper.Map<objTest, clsTest>(item));
    }
}

Its work fine, but is there anyway to map faster than this? Maybe is there any way mapping from List to List even my setting like above?

Thank you

You can just call Map with the list...

List<objTest> objListResult = Mapper.Map<List<clsTest>, List<objTest>>(objTest);

As you can see in documentation: http://automapper.codeplex.com/wikipage?title=Lists%20and%20Arrays

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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