title: Dynamic switch statement foreach
body: <p>I have just started this week to learn ASP.Net core 2.0 with EF Core and MVC using C#. So i am a complete Noob. </p>
  
  <p>Is they a way to create a dynamic Switch (SortOrder) using a dynamic parameters pulled from a Model/restful API? this is so my Switch statements don't end up 30+ cases deep. </p>
  
  <p>i am looking for something on the lines of this Sudo:</p>
  
  <pre><code>switch (sortOrder)
  {
      default:
          Tickets = Tickets.OrderBy(s =&gt; s.ID);
          break;
      foreach (string Tick in Tickets)
          {
          case Tick :
             Tickets = Tickets.OrderBy(T =&gt; Tick);
             break;
   }
  </code></pre>
  
  <p>for reference part of my TicketController</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Index(string sortOrder, string searchString)
  {
      //sorting
      ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "ID" : "";
      ViewData["DateSortParm"] = sortOrder == "ID" ? "Environment" : "Date";
      ViewData["CurrentFilter"] = searchString;
  
      var Tickets = from s in _context.MyTickets
                     select s;
      // search
      if (!String.IsNullOrEmpty(searchString))
      {
          Tickets = Tickets.Where(T =&gt; T.ID.ToString().Contains(searchString)
                                 || T.Environment.Contains(searchString)
                                 || T.YourRef.Contains(searchString)
                                 || T.Priority.Contains(searchString)
                                 || T.Type.Contains(searchString)
                                 || T.ProductName.Contains(searchString)
                                 || T.Environment.Contains(searchString)
                                 || T.Version.Contains(searchString)
                                 || T.Description.Contains(searchString)
                                 || T.Status.Contains(searchString)
                                 || T.Contact.Contains(searchString));
      }
      ////sorting
      //switch (sortOrder)
      //{
      //    case "ID":
      //        Tickets = Tickets.OrderByDescending(s =&gt; s.ID);
      //        break;
      //    case "Date":
      //        Tickets = Tickets.OrderBy(s =&gt; s.CreatedDate);
      //        break;
      //    case "Environment":
      //        Tickets = Tickets.OrderBy(s =&gt; s.Environment);
      //        break;
      //    default:
      //        Tickets = Tickets.OrderBy(s =&gt; s.ID);
      //        break;
      //}
  
      switch (sortOrder)
      {
          default:
              Tickets = Tickets.OrderBy(s =&gt; s.ID);
              break;
          foreach (string Tick in Tickets)
              {
                  case Col :
              Tickets = Tickets.OrderBy(T =&gt; Col)
         }
  
  
      }
  
  
      // return results
      return View(await Tickets.AsNoTracking().ToListAsync());
  }
  </code></pre>