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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:49:41+00:00 2026-05-26T02:49:41+00:00

EDIT: Simplified problem and clarified question. I am trying to convert this Stored Procedure

  • 0

EDIT: Simplified problem and clarified question.

I am trying to convert this Stored Procedure into a LINQ statement:

SELECT UserID, MAX(RowID) as RowID into #tempa
FROM Users
WHERE Approved = 1 
GROUP BY UserID

SELECT (bunch of columns)
FROM Users INNER JOIN #tempa
ON (Users.UserID = #tempa.UserID AND Users.RowID = #tempa.RowID)
DROP TABLE #tempa

When I run this SPROC I get 292 rows. When I try to convert it to LINQ I do the following:

IQueryable<User> userQuery = db.Users.Where(x => x.Approved == true);
        userQuery = userQuery.Where((x => x.RowID ==
                        db.Users.Where(u => u.UserID == x.UserID).Max(u => u.RowID)
           ));
         IList<User> users = userQuery.ToList();

Result is 293 rows…

Now I change the SPROC to (it’s a bitmask where value 2 is active):

SELECT UserID, MAX(RowID) as RowID into #tempa
FROM Users
WHERE Approved = 1 AND (UserAttribtues & 2) = 2
GROUP BY UserID

SELECT (bunch of columns)
FROM Users INNER JOIN #tempa
ON (Users.UserID = #tempa.UserID AND Users.RowID = #tempa.RowID)
DROP TABLE #tempa

I get 289 rows with the sproc.

Try the following with LINQ:

 IQueryable<User> userQuery = db.Users.Where(x => (x.UserAttributes & Convert.ToInt32(UserAttributes.Active)) == Convert.ToInt32(UserAttributes.Active) && x.Approved == true);
        userQuery = userQuery.Where((x => x.RowID ==
                        db.Users.Where(u => u.UserID == x.UserID).Max(u => u.RowID)
           ));

Result is 127 rows….

At first I thought it was my use of .Max in the subquery, which AD.NET corrected me below, but the numbers are way off still, what am I missing here? I’m not understanding something…

  • 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-26T02:49:41+00:00Added an answer on May 26, 2026 at 2:49 am

    Something like this should work, you can refactor out some predicates from this too.

     predicate = predicate.And((x => x.RowId == 
                                            db.Users
                                                 .Where(u=>u.UserId ==x.UserId)
                                                 .Max(u=>u.RowId)
                               ));
    

    You can try something like this:

    var result = from u in db.Users
    let max = db.Users.Where(au=>au.UserId == u.UserId).Max(au=>au.RowId)
    let maxUId = db.Users.Where(au=>au.UserId == u.UserId && au.RowId == max).Select(au=>au.UserId).FirstOrDefault()
    where u.RowId == max && u.UserId == maxUId
    select u
    

    Check that against the sql (strip out any other conditions please for debugging).
    I think I missed the userId part, you’ll need to match the userId as well, otherwise it should not work properly. I am sure you can make the query a bit better, for example, use let to keep the whole user object which has the highest rowid and then you can use the object to match both rowid and userid.

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

Sidebar

Related Questions

Edit: This question was written in 2008, which was like 3 internet ages ago.
EDIT: This question is more about language engineering than C++ itself. I used C++
This is the simplified version of the problem: We have a table on an
In this simplified version of my actual problem, I have two tables: User and
I've got a problem with foreign key. This is my DB structure (simplified): Table
I am trying to create a simplified code to insert images dynamically into a
[ Edit: I'm replacing the original, confusing question with a simplified example demonstrating the
I have simplified my problem to this example: #include <GL/glut.h> int main(int argc, char**
Edit: To be clear, I'm trying to use this kind of generator (i.e. with
Could someone please explain why this is happening. I have simplified my problem by

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.