linq query to return object with parameter

linq query to get highest id with datetime query

I need a linq query that returns a QuestionAnswer object with the datetimevalue given the previousAttemptID

Here is database table called "QuestionAnswers"


ID  (int)
previousAttemptID  (int)
userID (int)
questionID (int)
anwerID (int)
dateAnswered (datetime)



So something like



QuestionAnswer = db.QuestionAnswers.Where(r => r.previousAttemptID == previousAttemptID)  // then get the record with the max datetime stampe (field dateAnswered)







ToString1Asked:
Who is Participating?
 
BardobraveConnect With a Mentor Commented:
Try this:

QuestionAnswer = db.QuestionAnswers.Where(r => r.previousAttemptID == previousAttemptID).OrderBy(dateAnswered).First()
0
 
BardobraveConnect With a Mentor Commented:
EDIT: As you want the max datetime you'll need to order by DESC

QuestionAnswer = db.QuestionAnswers.Where(r => r.previousAttemptID == previousAttemptID).OrderByDescending(dateAnswered).First()
0
 
tvPrasadConnect With a Mentor Commented:
QuestionAnswer= db.QuestionAnswers.OrderByDescending(r => r.dateAnswered).Where(r => r.previousAttemptID == previousAttemptID).First();
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.