c# - Mapping separate fields into one string using Automapper -


i have spent couple of days trying track down without luck.

i have table of addresses. each row has 4 address fields. want map them object has 1 field single string made of 4 fields, (there's but), if 1 of fields contains null or empty string want ignore it.

e.g. address table contains :- address1 = house number address2 = street address3 = address4 = town

new object contain string :- house number, street, town.

as requested @ :-

the automapper config file

public static class automapperconfig {     public static void configure()     {         mapper.initialize(cfg =>         {             cfg.addprofile(new addresssearchlist_toresponse_profile());         });     } } 

the profile definition :

    public class addresssearchlist_toresponse_profile : profile {     protected override void configure()     {         mapper.createmap<address, addresssearchresponsedto>()             .convertusing<convertaddresstosearchlist>();          mapper.assertconfigurationisvalid();          //this far - missing      } } 

and conversion routine (admittedly not slickest code ever):

public class convertaddresstosearchlist : itypeconverter<address, addresssearchresponsedto> {      public addresssearchresponsedto convert(resolutioncontext context)     {          string newaddress = string.empty;         address oldaddress = (address)context.sourcevalue;         int addressid = oldaddress.id;          newaddress = oldaddress.addressline1;          if (!string.isnullorempty(oldaddress.addressline2))         {             newaddress += ", " + oldaddress.addressline2;         }          if (!string.isnullorempty(oldaddress.addressline3))         {             newaddress += ", " + oldaddress.addressline3;         }          if (!string.isnullorempty(oldaddress.addressline4))         {             newaddress += ", " + oldaddress.addressline4;         }          if (!string.isnullorempty(oldaddress.county))         {             newaddress += ", " + oldaddress.county;         }          if (!string.isnullorempty(oldaddress.postcode))         {             newaddress += ".  " + oldaddress.postcode;         }           addresssearchresponsedto searchaddress = new addresssearchresponsedto { id = addressid, address = newaddress };          return searchaddress;      } 

thanks

steve

maybe, it's me, (there's but), question?

i think it's converted, missing code mapping source entity , display result monitor, this:

addresssearchresponsedto result = mapper.map<address,addresssearchresponsedto>(source);  console.writeline(result.newaddress);  

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -