简体   繁体   中英

LINQ getting the latest entries

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 :)

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.

BoxID, ModuleID, PortNumber, Value,       Time
1,     0A,       1,          {Value},     {Date}

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:

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
             };

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.

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.

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.

@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.

http://i.stack.imgur.com/oX1ZK.jpg

Here's how it would look at my client side:

http://i.stack.imgur.com/stBXY.jpg

Obviously i would only like to display the latest Data. I hope this clears up the confusion.


I seem to have resolved my own issue but in a nonefficient way.

SQLdbDataContext sqlDB = new SQLdbDataContext();

                    var LatestObjects = (from q in sqlDB.CurrentData_Tabels select q).OrderByDescending(x => x.Time).First();

                    var selectedobjects = from q in sqlDB.CurrentData_Tabels
                                          where q.Time == LatestObjects.Time
                                          select q;


                    dgvLiveData.DataSource = selectedobjects;
                    dgvLiveData.RowHeadersVisible = false;  

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.

What you have done there is correct. You can write it in one query by making the statement for latestquery as subquery in the code, but it doesnt make any different. When you set the query to LatestQuery in your code, no execution is made to the server. The actual fetching is done when the data in the query is used.

Example:

  var q = dc.Mytable.Where(e=>e.id > 5);    // q is IQueryable, no fetch to db is executed yet
  q = q.Where(e=> e.name.StartWith("a"));   // q is still IQueryable and no fetch yet too
  var list = q.List();   // Fetching of data to the db happens here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM