title: Entity Framework linq orderby function
body: <p>I am new to Entity Framework and linq. I am working with asp.net mvc 5 and C#.
  I write a query with dynamic sorting as follows:</p>
  
  <pre><code>public static IEnumerable&lt;T&gt; OrderByDynamic&lt;T&gt;(this IEnumerable&lt;T&gt; source, string propertyName, bool Ascending)
  {
      if (Ascending)
          return source.OrderBy(x =&gt; x.GetType().GetProperty(propertyName).GetValue(x, null));
      else
          return source.OrderByDescending(x =&gt; x.GetType().GetProperty(propertyName).GetValue(x, null));
      }
  </code></pre>
  
  <p>and in my repository I can write:</p>
  
  <pre><code>string sortExpr = "name";
  _objDB.hotels.Where(s =&gt; (s.city_id = 1))
               .OrderByDynamic(sortExpr, Ascending).ToList();
  </code></pre>
  
  <p>This code works fine when sorting is on a column of a table, but I need to sort by a SQL function. I entered the function into the <code>.edmx</code> model with the following code</p>
  
  <pre><code>[EdmFunction("hotelReservation.Core.Data", "getHotelMinPrice_cap")]
  public static int getHotelMinPrice_cap(int Hotel_id, int capacity, DateTime enter_date, DateTime exit_date)
  {
      throw new NotSupportedException("Direct calls are not supported.");
  }
  </code></pre>
  
  <p>and my SQL selection is something like:</p>
  
  <pre><code>select * 
  from hotel
  where city_id = 1
  order by dbo.getHotelMinPrice_cap(hotel.id,1,'2001-01-01', '2021-01-01')
  </code></pre>
  
  <p>How can I write the last SQL query with dynamic sorting in linq?</p>