Reputation: 870
My object Box
has the property SerialNumbers
, which is a list (or ICollection) of <SerialNumber>
objects. I need each SerialNumber
's .Name
property to be mapped to a string in a list of strings in my BoxedElectrodesRowModel
.
Here is my code:
c.CreateMap<Box, BoxedElectrodesRowModel>()
.ForMember(dest => dest.BoxId, opts => opts.MapFrom(src => src.BoxID))
.ForMember(dest => dest.SerialNumbers, opts => opts.MapFrom(src => src.SerialNumbers))
.ForMember(dest => dest.DateCreated, opts => opts.MapFrom(src => src.DateCreated));
If you notice in the third line, I'm trying to convert dest.SerialNumbers
(which is a List of strings in the model) to src.SerialNumbers
which is an ICollection of SerialNumber
s. I specifically need the SerialNumber
's name property, though. I've tried to do src.SerialNumbers.Name
but LINQ doesn't like that.
My attempt to fix this is to add this code above the previous block:
c.CreateMap<SerialNumber, string>()
.ConvertUsing(src => src.Name);
But then I get the error "Cannot convert lambda expression to type 'string' because it is not a delegate type".
I'm really quite new to AutoMapper and feel like I'm flailing around in the dark. Can someone help me through this?
Upvotes: 1
Views: 875
Reputation: 2092
Just convert your source to list.
src.SerialNumbers.ToList()
If you need a nested property then select that property first.
src.SerialNumbers.Select(t=>t.Name).ToList()
Upvotes: 1