Solved

Linq query for top 1

Posted on 2011-09-27
6
236 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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