title: How can I select one record (ordered by descending)?
body: <p>I use LINQ-TO-SQL. And I have a control Order. And in the control I have a property OrderDetail. I only want to show the last updated data. So I have to order by ChangedAt. That's a DateTime field.</p>
  
  <p>When I try to open this entity. I get an error like <strong>Specified cast is not valid</strong>, while I see by debugging the selection has 1 row. </p>
  
  <p>How can I select one record, ordered by descending (ChangedAt)?</p>
  
  <p>The relationship between both entities Order : OrderDetail &lt;=> 1 : n.</p>
  
  <pre><code>partial class Order : ILock, ICloneable
  {
     ...
      public OrderDetail Detail
      {
          get
          {
              var detail = this.OrderDetails.Select(od =&gt; od);
  
              if (detail != null &amp;&amp; detail.Count() &gt; 0)
                  return detail.OrderByDescending(od =&gt; od.ChangedAt).FirstOrDefault();
              else
                  return null;
          }
      }
  </code></pre>
  
  <p>I don't think it's important. But I use SQL-Server as database.</p>