Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

linq query to return object with parameter

Posted on 2010-09-03
3
Medium Priority
?
393 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
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
Bardobrave earned 1336 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 1336 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 664 total points
ID: 33596087
QuestionAnswer= db.QuestionAnswers.OrderByDescending(r => r.dateAnswered).Where(r => r.previousAttemptID == previousAttemptID).First();
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline

916 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