Saturday, June 2, 2018

Assign ParentId of model using auto mapper

this is the sample class in aspnet core project web api

public class Comment { public int Id { get; set; } public int ParentId { get; set; } } 

And I have a List<Comment> where the first comment is the first entry, so once the Id is generated, I would like to assign the value of that Id to all the other Comments in the list to its ParentId property starting at index 1 onwards.

So i used mapper profile but this did not work

CreateMap<List<Comment>, Comment>() .AfterMap((a, c) => { for (int i=0; i<a.Count; i++) { if (i == 0) continue; a[i].ParentId = c.Id; } }); 

Any ideas?

Assign ParentId of model using auto mapper Click here
  • Blogger Comment
  • Facebook Comment

0 comments:

Post a Comment

The webdev Team