title: LINQ getting the latest entries
body: <p>I'm fairly new to LINQ, so this might be an easy peasy solution for some of you. I've been ripping my hair out for quite a while now :)</p>
  
  <p>Scenario:
  I have a box containing 3 Hardware Modules that have diffrent ports.
  Using Serial comunication I extract data from the modules into a SQL table called CurrentData_Tables
  So each time a unit gets produced, my program will write an entry into the table saying how big the sum is.
  Let me try and explain the data.</p>
  
  <pre><code>BoxID, ModuleID, PortNumber, Value,       Time
  1,     0A,       1,          {Value},     {Date}
  </code></pre>
  
  <p>The time is excatly the same on the readings for that specific module, so I imagined I could group by the data that is constant and then just get the latest.
  This is what I have so far:</p>
  
  <pre><code>SQLdbDataContext sqlDB = new SQLdbDataContext();
  BindingSource bs = new BindingSource();
  var Latest = from q in sqlDB.CurrentData_Tabels
               group q by new
               {
                   q.BoxID,
                   q.ModuleID,
                   q.PortNumber,
                   q.Time
               }
               into groupOrder
               select new
               {
                   BoxID = groupOrder.Key.BoxID,
                   ModuleID = groupOrder.Key.ModuleID,
                   Portnumber = groupOrder.Key.PortNumber,
                   Time = groupOrder.Key.Time
               };
  </code></pre>
  
  <p>And that's perfectly fine because I get all the results I want and more. I don't need to know that the reading on module 0A, port 1 had counted to 2 before the latest entry of 3.
  Does that make sense? If not please let me try to explain it again.</p>
  
  <p>Doing a > to get the largest sum wouldn't work since I also have statuses that trigger on bool values. One means there's an alarm present on the machine and zero means all is OK.</p>
  
  <p>I'm sorry for the double post. For whatever reason my temporary account was locked out and I couldn't comment on my old post. Now I'm registered and should hopefully be able to provide additional information.</p>
  
  <p>@Jon Skeet, Here's the setup with 2 active ports on Module 0B, Imagine that there had been an alarm active on module 0B port 1, then i would still get the outdated data shown even though the problem had been resolved.</p>
  
  <p><a href="http://i.stack.imgur.com/oX1ZK.jpg" rel="nofollow">http://i.stack.imgur.com/oX1ZK.jpg</a></p>
  
  <p>Here's how it would look at my client side:</p>
  
  <p><a href="http://i.stack.imgur.com/stBXY.jpg" rel="nofollow">http://i.stack.imgur.com/stBXY.jpg</a></p>
  
  <p>Obviously i would only like to display the latest Data. I hope this clears up the confusion.</p>
  
  <hr>
  
  <p>I seem to have resolved my own issue but in a nonefficient way.</p>
  
  <pre><code>SQLdbDataContext sqlDB = new SQLdbDataContext();
  
                      var LatestObjects = (from q in sqlDB.CurrentData_Tabels select q).OrderByDescending(x =&gt; x.Time).First();
  
                      var selectedobjects = from q in sqlDB.CurrentData_Tabels
                                            where q.Time == LatestObjects.Time
                                            select q;
  
  
                      dgvLiveData.DataSource = selectedobjects;
                      dgvLiveData.RowHeadersVisible = false;  
  </code></pre>
  
  <p>By combining 2 queries I got what I wanted. I'm sure this could be resolved using one query but I'm nowhere near being a hardcore programmer that would figure that one out.</p>