Solved

Linq query for top 1

Posted on 2011-09-27
6
234 Views
Last Modified: 2012-05-12
i am trying to retrieve the top 1 object from the following query but it is not working as expected. Please help.
Dim LastQuestion As Question = (From q In qu Order By q.QuestionNumber Descending Take 1 Select q)

Open in new window

0
Comment
Question by:suroma
  • 3
  • 2
6 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36709400
Try this


Dim LastQuestion As Question = (From q In qu Order By q.QuestionNumber Descending Select q).Take(1)
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36709426
Sorry it was c# syntax.
0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36709498
give us your definition of qu
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 36709554
Actually, you probably do not need to define that.
Take 1 means you are looking for and up to position 1, just use .First instead, e.g
Dim LastQuestion As Question = (From q In qu Order By q.QuestionNumber Descending Select q).First

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 36709744
Also your Question has to implement IComparable
0
 

Author Closing Comment

by:suroma
ID: 36715055
Works perfectly. Many thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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 …

932 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now