Solved

how to get top records using where condition in LINQ

Posted on 2009-04-02
6
1,235 Views
Last Modified: 2013-11-11
my query is

select top(1) * from tablename where col = value.

i need this query in LINQ.i tried in this way

from x in datacontext.tablename.take(1) where col == value select x.

but it's not getting values.

0
Comment
Question by:ravindra333
  • 3
  • 2
6 Comments
 
LVL 1

Accepted Solution

by:
SysPro earned 500 total points
ID: 24048720
Hello,
try this:
datacontext.tablename (x => x.col == value).Take(1);
SysPro
0
 
LVL 21

Expert Comment

by:naspinski
ID: 24049801
You are getting this because you are first 'taking' 1 record, and if it happens NOT to have a col == value (which it most likely will not) then it returns nothing.  You want to take it after like SysPro says.

Here are a few more ways with LINQ to get the same thing (I prefer 5 myself):
var results1 = (from x in datacontext.tablename where x.col == value select x).Take(1);
var results2 = (from x in datacontext.tablename where x.col == value select x).First();
var results3 = (from x in datacontext.tablename where x.col == value select x).Single();
 
//using lambdas (I like this better as it's cleaner looking code)
var results4 = datacontext.tablename.Where(x => x.col == value).Take(1);
var results5 = datacontext.tablename.First(x => x.col == value);
var results6 = datacontext.tablename.Single(x => x.col == value);

Open in new window

0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24056850
Try this:
from t in Transactions.Where(t=>t.StockpileID == 2033).Take(1) select t
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 21

Expert Comment

by:naspinski
ID: 24056896
sree ven, that will not work, you are mixing syntax
0
 
LVL 21

Expert Comment

by:naspinski
ID: 24056899
i apologize, that will work, but why stretch it out to be that long and confusing by mixing syntax?
0
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24058542
hello naspinski

was just correcting ravindra333's query,
as you said below will work pretty well too:

        var categories = GetCategories();
        List<Category> list = categories .Where(c => c.Name == "Camera").Take(1).ToList();
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export import database 4 51
HTTPS jquery doesn't work 9 58
Help with error when uploading excel file 3 26
jQuery - following an example but can I save data server side? 20 46
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 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