title: Ordering not working in Entity Framework query
body: <p>I'm currently battling a linq query for my application using Entity Framework (6.1.3)</p>
  
  <p>The query is as follows:</p>
  
  <pre><code>var productPeriods = (from pp in ctx.ProductPeriods                                          
    where pp.IsActive &amp;&amp; pp.Product.IsBuyBackForProduct == null &amp;&amp; !pp.Product.ProductAddOns.Any() &amp;&amp; pp.PowerRegionID == powerRegionId
    select new
    {
        ProductPeriod = pp,
        Price = pp.Prices
          .OrderByDescending(x =&gt; x.Created)
          .GroupBy(x =&gt; x.FirmID)
          .Select(pr =&gt; pr.FirstOrDefault())
          .OrderByDescending(x =&gt; x.ProductPrice)
          .FirstOrDefault()
    }).ToList();
  </code></pre>
  
  <p>The purpose of the query is to find the latest price from the prices collection of a product period, grouped by the firm ID and then select the best price of the latest prices from each firm.</p>
  
  <p>This works perfectly in Linqpad, but the first <code>OrderByDescending(x =&gt; x.Created)</code> doesn't work when used in context of Entity Framework.</p>
  
  <p>Does anyone knows why? And perhaps have a solution for it? :-)</p>
  
  <p>Thanks in advance!</p>
  
  <p><strong>Update</strong></p>
  
  <p>Thanks for all replies. I've tried the following:</p>
  
  <pre><code>select new {
      ProductPeriod = p,  
      Price = p.Prices.GroupBy(x =&gt; x.FirmID).Select(pr =&gt; pr.OrderByDescending(x =&gt; x.Created).ThenByDescending(x =&gt; x.ProductPrice).FirstOrDefault())
  }
  </code></pre>
  
  <p>But it seems like <code>ThenByDescending(x =&gt; x.ProductPrice)</code> gets ignored as well. The prices are not sorted correctly in the output. They're output like this:</p>
  
  <pre><code>Price: 0,22940, Created: 06-03-2015 10:15:09,
  Price: 0,23150, Created: 06-03-2015 10:05:48
  Price: 0,20040, Created: 06-03-2015 09:24:24
  </code></pre>
  
  <p><strong>Update 2 (solution for now)</strong></p>
  
  <p>I came to the solution that the initial query just returns the latest prices from each firm. There's currently three firms, so the performance should be alright.</p>
  
  <p>Later in my code, where I'm actually using the latest and best price, I simply do an <code>.OrderByDescending(x =&gt; x.ProductPrice).FirstOrDefault()</code> and check if it's not null.</p>
  
  <p>I.e:</p>
  
  <pre><code>var productPeriods = (from pp in ctx.ProductPeriods
                        where pp.IsActive &amp;&amp; pp.Product.IsBuyBackForProduct == null &amp;&amp; !pp.Product.ProductAddOns.Any() &amp;&amp; pp.PowerRegionID == powerRegionId
                        select new
                        {
                            ProductPeriod = pp,
                            Prices = pp.Prices.GroupBy(x =&gt; x.FirmID).Select(pr =&gt; pr.OrderByDescending(x =&gt; x.Created).FirstOrDefault())
                        }).ToList();
  </code></pre>
  
  <p>Later in my code:</p>
  
  <pre><code>var bestPriceOfToday = period.Prices.OrderByDescending(x =&gt; x.ProductPrice).FirstOrDefault()
  </code></pre>