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

  • SEARCH
  • Home
  • 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 8376027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:19:36+00:00 2026-06-09T15:19:36+00:00

I have the following object public partial class Log { public System.Guid Id {

  • 0

I have the following object

 public partial class Log
{
    public System.Guid Id { get; set; }
    public double Lat { get; set; }
    public double Lng { get; set; }
    public System.DateTime TimeStamp { get; set; }
    public int DeviceId { get; set; }
}

And I would like to get most recent log per device. So I have this

from log in allLogs
                   group log by log.DeviceId
                   into l
                   select new {DeviceId = l.Key, TimeStamp = l.Max(s => s.TimeStamp)};

This works great except that it only includes the deviceId and the TimeStamp. I would also like to include the Lat and Lng fields from the Log as well.

But note these fields are not part of the key or the aggregate function. I.E I don’t want to group by these fields I just want then to be in the result set. So that I have the lat Lng of the most recent log per device.

  • 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-09T15:19:38+00:00Added an answer on June 9, 2026 at 3:19 pm

    This would work in LINQ to Objects – I don’t know about LINQ to SQL etc:

    var query = from log in allLogs
                group log by log.DeviceId into logs
                select logs.OrderByDescending(x => x.Timestamp).First();
    

    In other words, sort each group, and take the latest.

    Alternatively, definitely only in LINQ to Objects, you could use MaxBy from MoreLINQ:

    var query = from log in allLogs
                group log by log.DeviceId into logs
                select logs.MaxBy(x => x.Timestamp);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following object: public class QueueItem { public long _id { get;
I have the following code... public partial class DownloadFile : System.Web.UI.Page { protected void
I have the following object: public partial class Game { public bool Finished {
I have the following code behind: public partial class _Default : System.Web.UI.Page { List<GlassesCollection>
Suppose I have the following asp.net page: public partial class SamplePage : System.Web.UI.Page {
Suppose I have the following service object public class UserService { @Autowired private UserDao
I'm using ASP.NET MVC2 and I have the following object structure: public class IDealer
I have the following database table object: public class Goal { @DatabaseField(generatedId = true)
I have the following method: public object[] GetEventsByUser(DateTime start, DateTime end, string fullUrl) The
I have the following converter: public class MyConverter : IValueConverter { public object Convert(object

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.