c# - How to ignore a list mapping when the list is empty but not null? -


how ignore list mapping when list empty not null.

if source.divisions (which ienumerable) null or empty des.divisions shouldn't mapped:

 mapper.createmap<model.event, datacontracts.event>()    .formember(des => des.divisions, e => e.mapfrom(source => source.divisions)) 

i've found below solution:

  mapper.createmap<model.event, datacontracts.event>()        .formember(des => des.divisions, e => {  e.condition(source => !source.divisions.isnullorempty())); e.mapfrom(source => source.divisions)); }); 

is there anyway simplify above further?

e.g creating extension method.

mapper.createmap<model.event, datacontracts.event>()            .formember(des => des.divisions, e => e.maplistifnotempty(source => source.divisions)); 

i wrote extension, hope helps!

 public static void maplistifnotempty<tsource, tmapfrom>(this imemberconfigurationexpression<tsource> map,         func<tsource, ienumerable<tmapfrom>> mapfrom)     {         map.condition(src => !mapfrom(src).isnullorempty());          map.mapfrom(mapfrom);     } 

and can use this:

 mapper.createmap<model.event, datacontracts.event>()                 .formember(des => des.divisions, e => e.maplistifnotempty(source => source.geographies)); 

Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -