0

In my code, i have switch case such as

switch(iSortCol) {
   case1: if(iSortDir="desc"){order1 = order1.OrderByDescending(x=>x.GROUPNAME);
          elseif(iSortDir="asc")order1 = order1.OrderBy (x=>x.GROUPNAME);

   case2: if(iSortDir="desc"){order1 = order1.OrderByDescending(x=>x.GROUPMASTERID);
          elseif(iSortDir="asc")order1 = order1.OrderBy (x=>x.GROUPMASTERID);}
   ....

   case80:

   case81:
}

The growing of "switch...case..." leads more maintain work for the code, i am thinking how to covert the code to Dictionary or strategy pattern for a better maintainability.

By the way, the property in order 1 such as "GROUPNAME","GROUPMASTERID" has different type E.g. the "GROUPNAME" in (x=>x.GROUPNAME) is string type, but the "GROUPMASTERID" in (x=>x.GROUPMASTERID) int type.

Could anyone give me some clues, really appreciated

joern
  • 27,354
  • 7
  • 90
  • 105
baby123
  • 1
  • 1
  • Something like this? : http://byatool.com/general-coding/orderby-using-a-property-name/ EDIT: Also put the language tag in there. Some people just look at certain languages. – kevhann80 Mar 05 '14 at 13:17
  • Cool. joern, it works fine with Dictionary>,Thanks! – baby123 Mar 06 '14 at 00:05
  • possible duplicate of [Converting a hardcoded switch statement into a dynamically loaded many-keys-one-value lookup](http://stackoverflow.com/questions/2921158/converting-a-hardcoded-switch-statement-into-a-dynamically-loaded-many-keys-one) – Paul Sweatte Sep 30 '14 at 02:14

0 Answers0