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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:40:36+00:00 2026-05-15T22:40:36+00:00

I probably won’t be able to explain this well enough, but here it goes…

  • 0

I probably won’t be able to explain this well enough, but here it goes…

I have a table similar to this:

Id | Foreign_Key_Id | A_Number | Some_Other_Info
1      1                100          Red
2      1                200          Blue
3      1                300          Orange
4      2                100          Green
5      2                200          Yellow
6      3                100          Brown

I want to get the max “A_Number” for a particular “Foreign_Key_Id” in this table, but also return the rest of the record so I have info from the “Some_Other_Info” column.

I’ve found I can accomplish this via a query like this:

from x in This_Table
group x by x.Foreign_Key_Id into g
orderby g.Max(x => x.A_Number) descending
select g.Where (x => x.Foreign_Key_Id == g.Key).OrderByDescending (x => x.A_Number).First()

Edit: Or possibly this more concise query:

from x in This_Table
group x by x.Foreign_Key_Id into g
let maxANumber = g.Max(x => x.A_Number)
orderby maxANumber descending
select g.Where(x => x.A_Number == maxANumber).First()

Although I’m not quite sure which record it would take if one Foreign_Key_Id had two records with the same “A_Number” value (which is possible in the table I am referring to, I would just need to take the most recent one).

Which in my example I believe would return:

Id | Foreign_Key_Id | A_Number | Some_Other_Info
3      1                300          Orange
5      2                200          Yellow
6      3                100          Brown

It seems like there has to be a way to do this in an easier fashion (that makes for a faster query than the one I came up with), I just can’t figure out how.

Thanks in advance.

  • 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-05-15T22:40:36+00:00Added an answer on May 15, 2026 at 10:40 pm

    The following also works. You can order inside the group:

    from x in This_Table
    group x by x.Foreign_Key_Id into g
    select g.OrderByDescending(y => y.A_Number).First();
    

    Taking the most recent record when A_Numbers are equal is as simple as adding another OrderBy to the query.

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

Sidebar

Related Questions

Probably a stupid question, but here goes. In my view, I have the following
Probably missing something completely obvious here, but here goes. I'm starting out with Spring
Right now I'm building a website. Here's the link: http://theoew.uuuq.com/home/ This probably won't make
Probably really easy for a pro, but could someone re-write this from it's PHP
Probably it has been asked before but I cannot find an answer. Table Data
We are porting an MFC application to WPF, and will probably won't have time
This probably won't make sense without an example. I'm using python-disqus in my Django
I'm new to Python and after this one script I probably won't work with
It probably won't matter for my current utility, but just for good coding practice,
I know this is probably one of those you just have to try it

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.