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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:54:32+00:00 2026-05-23T14:54:32+00:00

I have a problem that I know how to solve in SQL but not

  • 0

I have a problem that I know how to solve in SQL but not with Linq to Entities.

My data looks like this:

ID    GROUP   TIMESTAMP
--    -----   ---------
1     A       2011-06-20
2     A       2011-06-21
3     B       2011-06-21
4     B       2011-06-22
5     B       2011-06-23
6     C       2011-06-30

I want to retrieve all the Entity objects (not just the ID) such that I am only getting the most recent record from each group. (ie. the records with ids 2, 5, 6)

In SQL I would do something like this:

SELECT * FROM my_table a
WHERE a.timestamp = 
   (SELECT MAX(timestamp) FROM my_table b
   WHERE a.group = b.group)

(For the sake of this question you can assume that timestamp is unique within each group).

I’d like to do this query against a WCF Data Service using Linq to Entities but I can’t seem to have a nested query that references the outside query like this. Can anyone 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-05-23T14:54:32+00:00Added an answer on May 23, 2026 at 2:54 pm

    Possibly not as clean and efficient as the hand written version but here’s what I came up with

        var q = from a in db.MyEntities
                where a.Timestamp == (from b in db.MyEntities
                                      where b.Group == a.Group
                                      select b.Timestamp).Max()
                select a;
    

    which translates into this SQL

    SELECT
    [Project1].[Id] AS [Id],
    [Project1].[Group] AS [Group],
    [Project1].[Timestamp] AS [Timestamp]
    FROM ( SELECT
            [Extent1].[Id] AS [Id],
            [Extent1].[Group] AS [Group],
            [Extent1].[Timestamp] AS [Timestamp],
            [SSQTAB1].[A1] AS [C1]
            FROM [MyEntities] AS [Extent1]
             OUTER APPLY
            (SELECT
                    MAX([Extent2].[Timestamp]) AS [A1]
                    FROM [MyEntities] AS [Extent2]
                    WHERE [Extent2].[Group] = [Extent1].[Group]) AS [SSQTAB1]
    )  AS [Project1]
    WHERE [Project1].[Timestamp] = [Project1].[C1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that someone introduced a bug but I do not know
i have a problem, that i don't know how to solve it. i have
I have a problem that I would like have solved via a SQL query.
i would like to have a query that will solve my problem in native
So I have a problem that I don't really know how to go about.
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
I'd like to make a textbox that accepts only numbers, but not integer, but
Apologies for the fuzzy title... My problem is this; I have a SQL Server
I have a problem with my cursor which I know the error of, but
this post is a long one sorry for that but the problem is complex

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.