Mapping Lists using Automapper

后端 未结 5 1624
挽巷
挽巷 2020-12-02 17:52

I have the classes:

public class Person{ /* Props here */ }

public class PersonViewModel { /* Props here */ }

Then the list:



        
5条回答
  •  遥遥无期
    2020-12-02 18:36

    If you're using IQueryable lists here (from EF or NH, for example) you can use the AutoMapper.IQueryableExtensions methods, Project() and To().

    This is my first time with AutoMapper, but I'm succeeding by creating a map for just the model:

    Mapper.CreateMap();
    Mapper.AssertConfigurationIsValid();
    

    And then using the IQueryableExtension methods Project() and To():

    using AutoMapper.QueryableExtensions;
    ...
    
    IQueryable people = new List().AsQueryable(); //actually from ORM
    IQueryable peopleVM = people.Project().To();
    

提交回复
热议问题