public static void Main(string[] args) { var list = new Dto[20]; for (var i = 0; i < 20; i++) { list[i] = new Dto { Id = i + 1, Name = $"Name {i + 1 }", Date = DateTime.Now }; } var sw = new Stopwatch(); sw.Start(); var source = list.Select(x => new { x.Id, x.Name, x.Date }).ToArray(); var destination = new Dto[source.Length]; var properties = source .GetType() .GetElementType() .GetProperties() .Where(p => p.PropertyType.IsValueType || p.PropertyType == typeof(string)).ToArray(); for (var i = 0; i < source.Length; i++) { var dto = Activator.CreateInstance <Dto>(); foreach (var property in properties) { var destinationProperty = typeof(Dto).GetProperty(property.Name); if (destinationProperty != null) { var value = property.GetValue(source.ElementAt(i)); destinationProperty.SetValue(dto, value); } } destination[i] = dto; } sw.Stop(); Console.WriteLine(sw.ElapsedMilliseconds); Console.ReadKey(); }
public BookingDetail SetModelProperties() { DomainModel.Schedule = Context.ScheduleRepo.LoadById(ScheduleID); DomainModel.Passenger = Dto.Select(o => new PassengerDetail() { Address = Context.AddressRepo.LoadById(o.AddressID), DateOfBirth = o.DateOfBirth, Firstname = o.FirstName, Lastname = o.LastName, PassportNo = o.PassportNumber }).ToList(); return(DomainModel); }
public BookingDetail SetModelProperties() { _generalRepository = Factory.GetObject <IGeneralRepository <FlightSchedule> >(); IGeneralRepository <Address> _AddressRepository = Factory.GetObject <IGeneralRepository <Address> >(); DomainModel.Schedule = _generalRepository.LoadById(ScheduleID); DomainModel.Passenger = Dto.Select(o => new PassengerDetail() { Address = _AddressRepository.LoadById(o.AddressID), DateOfBirth = o.DateOfBirth, Firstname = o.FirstName, Lastname = o.LastName, PassportNo = o.PassportNumber }).ToList(); return(DomainModel); }