Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8566977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:46:44+00:00 2026-06-11T17:46:44+00:00

Some time ago I asked the question SQL Server: How do I maintain data

  • 0

Some time ago I asked the question SQL Server: How do I maintain data integrity using aggregate functions with group by? I got a great answer there, but now the problem has come up again, with Linq to SQL this time rather than plain SQL.

Backstory: I have a table full of gps data, which looks like this:

GPS_id, user_id, latitude, longitude, server_time, device_time

I used the following linq query to pull out the most recent gps record for a certain set of users:

var query =
    from gps in db.gps_data
    where (from u in db.users
        select u.user_id).Contains(gps.user_id)
    group gps by gps.user_id into groupedGPS
    select groupedGPS;

I then looped through it like so, but I had to order it first in order the properly grab the “most recent record”.

foreach (var gpsItem in query) {
    var ordered = gpsItem.OrderByDescending(g => g.device_time);
    list.Add(ordered.First());
}

This gave me what I needed, but at any time I have 100+ users all with 500+ gps records (and all of them were being accessed this way), so this code was taking 10+ seconds which I deemed unacceptable.

I then changed it to the following

var query =
    from gps in db.gps_data
    where (from u in db.users
        select u.user_id).Contains(gps.user_id)
    group gps by gps.user_id into groupedGPS
    select new 
    {
        GPS_id = groupedGPS.Max(x => x.GPS_id),
        user_id = groupedGPS.Max(x => x.user_id),
        latitude = groupedGPS.Max(x => x.latitude),
        longitude = groupedGPS.Max(x => x.longitude),
        server_time = groupedGPS.Max(x => x.server_time),
        device_time = groupedGPS.Max(x => x.device_time) 
    };

This query did seem faster, because as far as my understanding goes all of the unnecessary data is never actually loaded into memory. However, as in my original question of several months ago, I’ve lost my data integrity this way. There’s no guarantee that I’m seeing the most recent record, just the maximum value for all the fields in the grouping. This has no effect on most of the fields, but the latitude and longitude are almost always incorrect, as they are merely the max() records found in the grouping rather than the most recent ones.

How do I get around this issue? I realize I have the first solution retrieving me the correct data, but the amount of time it takes is far too long.

Thanks for the help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T17:46:45+00:00Added an answer on June 11, 2026 at 5:46 pm

    As far as I understand your question (newest record per user id), this would seem like it would do what you’re looking for;

    var q =
        from gps in db.gps_data
        where (from gps2 in db.gps_data
               group gps2 by gps2.user_id
               into g
               select new {a = g.Key, b = g.Max(f => f.server_time)})
               .Contains(new {a = gps.user_id, b = gps.server_time})
        select gps;
    

    It may give you duplicates if there are multiple readings at the same time for a user, I’m assuming they’re not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked this question about using the system-drawables in an Android-app some time ago:
Some time ago I was asked the strange question how would I implement map
I asked a question some time ago on java 2d pathfinding... Pathfinding 2D Java
This question was asked quite some time ago, and while it covers possible solutions
I asked another SOAP related question some time ago, which asks for a working
This is an extension to a question which I asked some time ago. I
I've already asked question similar to this some time ago, I thought I solved
Some time ago I asked a question about nested loops on SO and as
I'v asked these question some time ago: Multiple inheritance casting from base class to
this is somewhat the same question as I've asked some time ago: How to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.