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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:50:31+00:00 2026-05-16T08:50:31+00:00

This question is similar to this one: LINQ to SQL: GroupBy() and Max() to

  • 0

This question is similar to this one:

LINQ to SQL: GroupBy() and Max() to get the object with latest date

But I have a table which I want to return a lot of rows from. It’s primary key is used as a foreign key in a second table. For each row returned from the first row, I also want to return (in the same result set) the latest (in this case, ordered by a datetime field) 1 row.

How would this be constructed in LINQ using C#?

The best I have is:

    var instances = from i in ContextDB.Instances
                    join p in ContextDB.Points on i.InstanceID equals p.InstanceID
                    where categoryIDs.Contains(j.CategoryID)
                    group p by p.InstanceID into latestPoint
                    select new latestPoint.OrderBy(i => i.Instance.Title).FirstOrDefault();

But this is clearly wrong. Although I think I’m understanding group correctly I don’t think I’m understanding into correctly.

[EDIT] To avoid confusion, I think this question can be summed up as “How to do rows from one table along with the latest 1 row for each from another table?”

  • 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-16T08:50:31+00:00Added an answer on May 16, 2026 at 8:50 am

    How about:

    var instances = from i in ContextDB.Instances
                    where i.CategoryInstances.Any(categoryIDs.Contains(ci => ci.CategoryID))
                    select new
                    {
                        Instance = i,
                        LatestPoint = i.Points.OrderBy(i => i.Instance.Title).FirstOrDefault()
                    };
    

    Note that I can remove a lot of redundancy by using associations instead of join.

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

Sidebar

Related Questions

This question is similar to LINQ group one type of item but handled in
Note this question is similar this one except I'm not working with linq-to-sql, so
This question is similar to this one, but with an extra wrinkle: Auto-removing all
my question is similar to this one , but I don't want to use
My question is similar to this one , but I would like to replicate
This question is similar to this one , but hopefully it will pick up
I have a question that is similar, but not identical, to the one answered
My question is similar to this one and this one , but there is
This question is similar to another one I asked , but whereas in that
This question is similar to this one Fastest method of screen capturing but for

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.