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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:46:56+00:00 2026-05-20T23:46:56+00:00

How do I convert the following SQL statement into Lambda Expression or Linq Query?

  • 0

How do I convert the following SQL statement into Lambda Expression or Linq Query?

The following query get the single most recent Answer for each Question. Or to phrase it another way, get each Question with the newest Answer.

Also this will be execute by Entity Framework.

SELECT Answers.*
FROM Answers
Where AnswerID IN
(
    SELECT Max(AnswerID) AnswerID
    FROM Answers
    GROUP BY QuestionID
)

Here another way to look at the previous query using an Inner Join

SELECT answers.* 
FROM answers 
INNER JOIN  
(
     SELECT Max(answerID) answerID --,  QuestionSiteID
     FROM answers
     GROUP BY QuestionID 
) t ON
     answers.answerID = t.answerID  

I have read that the LINQ Contains method is sub optimal for queries that access SQL.
LINQ to Sql and .Contains() trap.

  • 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-20T23:46:57+00:00Added an answer on May 20, 2026 at 11:46 pm

    I think you could do this using something like:

     var subQuery = from a in answers
                    group a by a.QuestionID into grouping
                    select new
                    {
                        QuestionID = grouping.Key,
                        MaxAnswerID = grouping.Max(x => x.AnswerID)
                    };
    
     var query = from a in answers
                 from s in subQuery
                 where a.AnswerID == s.MaxAnswerID
                 select a;
    

    This results in a CROSS JOIN in the generated SQL


    Also, you could use join in the second part of the query:

     var query = from a in answers
                 join s in subQuery on a.AnswerID equals s.MaxAnswerID
                 select a;
    

    This results in a INNER JOIN in the SQL


    Note for side cases – the above answers make the reasonable assumption that AnswerID is the primary key of Answers – if you happen to have instead a table design which is keyed on (AnswerID, QuestionID) then you will need to join by both AnswerID and QuestionID like:

     var subQuery = from a in answers
                    group a by a.QuestionID into grouping
                    select new
                    {
                        QuestionID = grouping.Key,
                        MaxAnswerID = grouping.Max(x => x.AnswerID)
                    };
    
     var query = from a in answers
                 from s in subQuery
                 where a.AnswerID == s.MaxAnswerID
                 && a.QuestionID == s.QuestionID
                 select a;
    

    See the comment trail for more discussion on this alternate table design…

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

Sidebar

Related Questions

How do you convert a SQL query with nested SELECT statements to a LINQ
I have following sql query for finding 2nd max salary. Select * From Employee
I've the following SQL statement to pick a random value which doesn't exist in
I am trying to get some SQL to execute but am getting the following
I have the following scopes in my Job model and it seems there's a
Suppose that I have a table with the following schema: tableId field1 field2 .....
In Oracle, there's a view called V$SQLAREA that lists statistics on shared SQL area
In my stored procedure I declared two table variables on top of my procedure.
i am creating a student information system in php and the database looks as
In Short: I'm using VB.NET 2008 to connect to a Visual Foxpro 6 Database

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.