Sub=newSubSource1 { Age ="253"}, c="c3", GetA="A3", SetB="B3"};varsourceList =newList<Source1>{ source1, source3 };vardestViewModelList = sourceList.MapToList<Source1,Dest1>(); 运行结果: 以上就是我个人所得,如有错误,欢迎大家指正。 //2017.12.4 修改:destination和source写反了,...
c ="c", GetA ="A", SetB ="B"};varsource3 =newSource1 { Name ="source3", Sub =newSubSource1 { Age ="253"}, c ="c3", GetA ="A3", SetB ="B3"};varsourceList =newList<Source1> { source1, source3 };vardestViewModelList = sourceList.MapToList<Dest1, Source1>(); AI代...
.ForMember(dto=> dto.NAME_C1, (map) => map.MapFrom(m =>m.TM_WORKSHOP.TM_PLANT.NAME_C)) .ForMember(dto=> dto.UlocName, (map) => map.MapFrom(m =>m.TM_ULOC.NAME)) .ForMember(dto=> dto.ArtName, (map) => map.MapFrom(m =>m.TM_ART_LINE.NAME_C)) .ForMember(dto=> dto...
var config = new MapperConfiguration(cfg => cfg.CreateMap<TDestination, TSource>()); var mapper = config.CreateMapper(); return mapper.Map<List<TDestination>>(source); 同样的使用方式: var source1 = new Source1 Name = "source", Sub = new SubSource1 Age = "25" , c = "c", GetA...
(x=>x).ToList());cfg.CreateMap<SourceObject,DestinationObject>().ForMember(dest=>dest.FlatList,opt=>opt.MapFrom(src=>src.NestedList));});// 创建映射器varmapper=newMapper(config);// 进行对象转换varsource=newSourceObject{NestedList=newList<List<int>>{newList<int>{1,2,3...
lstEFModel= context.Set<TM_STATION>().AsNoTracking().ToList(); }//3.开启计时,使用AutoMapper转换对象System.Diagnostics.Stopwatch sw =newSystem.Diagnostics.Stopwatch(); sw.Start();varlistd = Mappers.Map<List<DTO_TM_STATION>>(lstEFModel); ...
{ _mapper = mapper; } public UserDto MapUsersToDto(List<User> users, List<UserRole> userRoles) { var userDtoList = _mapper.Map<List<UserDto>>(users); var userRoleDict = userRoles.GroupBy(ur => ur.UserId) .ToDictionary(g => g.Key, g =>...
获取学生和课程的数据 var students = dbContext.Students.Include(s => s.Courses).ToList(); var courses = dbContext.Courses.Include(c => c.Students).ToList(); // 将学生和课程转换为DTO对象 var studentDtos = mapper.Map<List<StudentDto>>(students); var courseDtos = mapper.Map<List<Course...
varpersons=newList<Person>{newPerson{Name="张三",Age=18},newPerson{Name="李四",Age=20},newPerson{Name="王五",Age=22}};varpersonDtos=persons.Select(p=>Mapper.Map<PersonDto>(p)).ToList(); 在上面的示例中,我们使用Select方法对persons列表进行投影映射,自动将Person对象转换为PersonDto对象。
public static TDestination MapTo<TDestination, TSource>(this TSource source)where TDestination : class where TSource : class { if (source == null) return default(TDestination);var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());