c# - is there any linqish way to save user form data - Re factoring my code -


i have simple form takes user data , save database. while working fine seems pretty repetitive.. not sure if can turn code in way checks user input , save in db.

here code..

public void saveuserdata()       {             mwcompetitioncontestantsdetails user = new mwcompetitioncontestantsdetails();             mwcompetitionsentrydetails entry = new mwcompetitionsentrydetails();              if (!isnullorempty(firstnametext.value))                   user.firstname = firstnametext.value;             if (!isnullorempty(lastnametext.value))                   user.lastname = lastnametext.value;             if (!isnullorempty(emailtext.value))                   user.useremailaddress = emailtext.value;             if (!isnullorempty(address1text.value))                   user.address1= address1text.value;             if (!isnullorempty(address2text.value))                   user.address2 = address2text.value;             if (!isnullorempty(citytext.value))                   user.city = citytext.value;             if (!isnullorempty(provincetext.value))                   user.province= provincetext.value;             if (!isnullorempty(postcodetext.value))                   user.postalcode = postcodetext.value;             if (!isnullorempty(countrytext.selecteditem.text))                   user.country = countrytext.selecteditem.text;              user.save();       }        public static bool isnullorempty<t>(t value)       {             if (typeof(t) == typeof(string))                   return string.isnullorempty(value string);              return value.equals(default(t));       }  

rather looking isnullorempty(data) on every fields, there suggestion improve code using linq or anything..

your time appreciated...

first of all, i'd like oniant's , adil mammadov's solutions deadly simple.
i've written code of fancy-shmancy approach use in similar situations , i'd share it:

      class save<t> : isave     {         private readonly system.action<t> _assignvalue;         private readonly system.func<t> _getvalue;          public void do()         {             t value = _getvalue();             if (!isnullorempty(value))             {                 _assignvalue(value);             }         }          public static save<t> value<t>(system.func<t> getvalue, system.action<t> assignvalue)         {             return new save<t>(getvalue, assignvalue);         }          private save(system.func<t> getvalue, system.action<t> assignvalue)         {             _getvalue = getvalue;             _assignvalue = assignvalue;         }          private static bool isnullorempty<t>(t value)         {             if (typeof(t) == typeof(string))                 return string.isnullorempty(value string);              return value.equals(default(t));         }     }      internal interface isave     {         void do();     }      public static void saveuserdata()     {         mwcompetitioncontestantsdetails user = new mwcompetitioncontestantsdetails();         mwcompetitionsentrydetails entry = new mwcompetitionsentrydetails();          new list<isave>         {             save<string>.value( () => firstnametext.value, x => user.firstname = x),             save<string>.value( () => lastnametext.value, x => user.lastname = x),                           save<int>.value( () => age.value, x => user.age = x),// int's supported :)                           // etc         }         .foreach(x => x.do());         user.save();     } 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -