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 8362759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:00:57+00:00 2026-06-09T12:00:57+00:00

I am stuck on this. I have two tables: Users2Users and ActivityLog. I want

  • 0

I am stuck on this. I have two tables: Users2Users and ActivityLog. I want to look up the list of Users2Users that are a user’s friends and return each of their latest activity based on its Timestamp, then check what type it is.

Users2Users
int UserId
int TypeOfLink
int FriendId

ActivityLog
int Type
int UserId
DateTime Timestamp

The thing that seems to be troubling me is getting the ActivityLog entity rather than its Timestamp value.

My friendList query looks like this:

var friendList = (from u in db.Users2Users
where u.UserId == userId || u.FriendId == userId
&& u.TypeOfLink == 2 // confirmed as friend
orderby u.User.ScreenName ascending
select u).Distinct().ToList();

After that I was going to try a foreach, but how do I return the ActivityLog entity rather than the Timestamp?

// get their last activity
foreach (var user in domusers)
{
    var act = (from a in db.ActivityLogs
    where a.UserId == user.UserId
    select a.Timestamp).Max();
    // other stuff
}
  • 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-09T12:00:58+00:00Added an answer on June 9, 2026 at 12:00 pm

    You can order the entries by Timestamp and the pick the first element:

    var act = db.ActivityLogs
      .Where(a => a.UserId == user.UserId)
      .OrderByDescending(a => a.Timestamp)
      .FirstOrDefault();
    

    Or using query syntax:

    var act = (from a in db.ActivityLogs 
    where a.UserId == user.UserId
    orderby a.Timestamp descending
    select a).FirstOrDefault();
    

    Note that if there are no elements in the source collection you will get a null result.

    Using OrderByDescending is slightly ineffecient because it will sort all the activities. It is more efficient to perform a single pass over the collection and find the element with the latest Timestamp. You can do that using Aggregate. Here I assume that you are able to create a “null” Activity where the Timestamp property has the default (and minimal value) for DateTime:

    var nullActivity = new Activity(); // nullActivity.Timestamp == DateTime.MinValue
    var activity = db.ActivityLogs
       .Where(a => a.UserId == user.UserId)
       .Aggregate(
         nullActivity,
         (accumulate, value) => value.Timestamp >= accumulate.Timestamp ? value : accumulate
       );
    if (activity != nullActivity)
      // The latest activity was found
    

    If you can’t create a “null” activity as the seed for Aggregate you can use an anonymous type to keep track of the latest timestamp and the associated activity but it is slightly more tedious to do.

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

Sidebar

Related Questions

this site is like an ecommerce site and i have two tables that i'm
I'm stuck in this problem for quite while now. I have two tables ItemMaster
I have this two tables from my program to manage stock: Articulos (items): id_articulo
This problem is giving me a real headache. I have two tables in my
High level I have two tables that need to have some of the data
I have a SQL script that is setting up two database tables with their
I have two tables like this: Table1ID Table2ID Table1ID SomeDate -------- ------------------------------ 1 1
I have been stuck on this for a while now and I cannot seem
I have been stuck on this for days, and was wondering if anyone had
so i have been stuck with this issue for 3 weeks now and i

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.