title: LINQ making orderbydesc generic
body: <p>I have written the <code>LINQ</code> query as following but just wondering if i can make it more generic and get rid of the hard coded values.</p>
  
  <pre><code>switch (sortOrder)
  {
      case "DESC":
          if(sortBy == "A")
              query = query.OrderByDescending(x =&gt; x.Field1);
          if(sortBy == "B")
              query = query.OrderByDescending(x =&gt; x.Field2);
          if (sortBy == "C")
              query = query.OrderByDescending(x =&gt; x.Field3);
          break;
      default:
          if(sortBy == "A")
              query = query.OrderBy(x =&gt; x.Field1);
          if(sortBy == "B")
              query = query.OrderBy(x =&gt; x.Field2);
          if (sortBy == "C")
              query = query.OrderBy(x =&gt; x.Field3);
          break;
  }
  </code></pre>
  
  <p>The link mentioned as duplicate is not the answer i am looking for. In that question user asked by sorting 1st of ASC and then Desc.</p>
  
  <p>In my case i have to either sort ASC or DESC.</p>