LINQ Select and populate List<T>

T.Zacks 3,996 Reputation points
2022-03-19T15:00:26.83+00:00

I know this is Microsoft forum and AutoMapper is not from Microsoft. just by curiosity i am asking a question about AutoMapper. if anyone use in there every day coding then please guide me. thanks

This way we can populate List<T> using LINQ

var dest = await dbContext.Destinations
   .Where(d => d.Id = id)
   .Select(d => new Dest {
       Thing = source.Thing,
       Thing2 = source.Thing2,
       FooBarBaz = source.Foo.Bar.Baz.
   })
   .ToList();

i read a article about usage of Automapper to populate List<T>

var dest = await dbContext.Destinations
   .Where(d => d.Id = id)
   .ProjectTo<Dest>() // oh so pretty
   .ToList();

both are same. i like to know the advantage when to use Automapper?

when dealing with huge data then if i use Automapper then execution speed will be better or it will be just like LINQ.Select() ?

just got another approach where this way populating LIST<T> by automapper is

roles.ToList().Select(role =>
Mapper.Map<Role, RoleDto>(role))
.ToList();

here Mapper.Map() is used but in first sample they use ProjectTo() which one is best?

if Role & RoleDto field names are not same then how map field name before mapping data?

Thanks

Developer technologies C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.