Solved

linq query to return object with parameter

Posted on 2010-09-03
3
385 Views
Last Modified: 2013-11-11
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)







0
Comment
Question by:ToString1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Bardobrave earned 334 total points
ID: 33596016
Try this:

QuestionAnswer = db.QuestionAnswers.Where(r => r.previousAttemptID == previousAttemptID).OrderBy(dateAnswered).First()
0
 
LVL 19

Assisted Solution

by:Bardobrave
Bardobrave earned 334 total points
ID: 33596028
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
 
LVL 5

Assisted Solution

by:tvPrasad
tvPrasad earned 166 total points
ID: 33596087
QuestionAnswer= db.QuestionAnswers.OrderByDescending(r => r.dateAnswered).Where(r => r.previousAttemptID == previousAttemptID).First();
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question