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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:26:07+00:00 2026-05-20T22:26:07+00:00

I have a somewhat simple query in SQL that I would like to convert

  • 0

I have a somewhat simple query in SQL that I would like to convert to Linq to Entities.

Basically, I want all of the available courses in the database, but I want the last test result for each course for a specific user if they have taken the course. They may have taken a course multiple times, but I would only want the last test result for each test. Is this possible with one Linq statement?

Here is the SQL:

select c.courseid, c.Name, ca.result
from Course c
    left join CourseAttempt ca
    on c.CourseId = ca.CourseId
        and ca.CourseAttemptId in
        (
            select max(courseattemptid)
            from courseattempt
            where userid=1234
            group by courseid
        )

Here is what I have so far:

var stuff = (from c in context.Course
             join ca in context.CourseAttempt.Where(a => a.userid == _userid) 
             on c.CourseId equals ca.Course.CourseId into jca
             select new
             {
                 courseId = c.CourseId,
                 name = c.Name,
                 result = jca.Select(a => a.result)
             }).ToList();

The problem is that result is an array of all of the results. How do I only get the last result?

  • 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-20T22:26:07+00:00Added an answer on May 20, 2026 at 10:26 pm

    This solution works:

    var stuff = (from c in context.Course
                 join ca in context.CourseAttempt.Where(a => a.user.userid == _userid) 
                 on c.CourseId equals ca.Course.CourseId into jca
                 select new
                 {
                     courseId = c.CourseId,
                     name = c.Name,
                     result = jca.OrderByDescending(a => a.CourseAttemptId).Take(1).Select(a => a.result).Cast<int>().DefaultIfEmpty(-1).FirstOrDefault()                 
                 }).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a somewhat complex query that I want to use as a source
I have a (somewhat) simple question about android. I have some classes that contain
I'm writing a very simple text editor and have run into a somewhat minor
I have several somewhat separate programs, that conceptually can fit into a single project.
I have a few tables with 70-80 columns in them. I would like to
I have this simple captcha script from online. I like it, and I edited
I am somewhat new to Django and have searched for some simple examples of
So I have a JavaScript array going somewhat like screw cap bottle [tech.] nonreturnable
I have a question about Django, unixODBC, FreeTDS, Apache2, mod_wsgi, that is somewhat similar
I have somewhat simple problem with my Flash projects. I've installed Flash CS5, created

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.