c# - automapper create.map() to a collection property -
how map collection of property interested last record of collection.
say, like.
public class itemdto <-- destination class { public string name { get; set; } public decimal pricesprice { get; set; } } public class item <-- source class { public int id { get; set; } public string name { get; set; } public virtual icollection<price> prices { get; set; } } public class price <-- source class { public int id { get; set; } public int itemid { get; set; } public decimal price { get; set; } public virtual item item { get; set; } }
then have tried this, doesn't seems right.
mapper.createmap<item, itemdto>() .formember(dto => dto.pricesprice, opt => opt.mapfrom(s => s.prices.lastordefault().price));
edit: , after did projection because if use mapper.map()
return entire result set not want, want values needed. did this:
project().to<itemdto>()
well, basically, want this:
from item in somedbcontext.items item.itemid == 1 select new itemdto { name = item.name, pricesprice = item.prices.lastordefault() }
can above code done using automapper
?
i think understand trying now.
try this
from item in somedbcontext.items item.itemid == 1 select mapper.map<itemdto>(item)
or use linq
somedbcontext.items.where(i=> i.itemid == 1).select(mapper.map<itemdto>)
both of these return list of itemdto object itemid 1
Comments
Post a Comment