Using Automapper, I want to map a property that is a List of type Employee using string.Join() to product a comma-delimited string of the names of an employee's rights. Here are the classes I'm using:
public class MappedEmployee
{
public string Name { get; set; }
public string RightNames { get; set; }
}
public class Employee
{
public string Name { get; set; }
public List<Right> Rights { get; set; }
}
public class Right
{
public string Name { get; set; }
}
And here is the code I have:
Mapper.CreateMap<Employee, MappedEmployee>()
.ForMember(d => d.RightNames, o => o.MapFrom(s => s.Rights.SelectMany(r => string.Join(", ", r.Name))));
var employee = new Employee
{
Name = "Joe Schmoe",
Rights = new List<Right>
{
new Right { Name = "Admin" },
new Right { Name = "User" },
}
};
var mappedEmployee = Mapper.Map<Employee, MappedEmployee>(employee);
However, this it's producing the folowing:
System.Linq.Enumerable+<SelectManyIterator>d__14`2[Employee.Right,System.Char]
What can I do do get a comma-delimited string of the Employee's rights?
Try using ResolveUsing
instead and putting string.Join
before the selection:
Mapper.CreateMap<Employee, MappedEmployee>()
.ForMember(d => d.RightNames, o => o.ResolveUsing(s => string.Join(", ",s.Rights.Select(r => r.Name))));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.