Solved

how to get top records using where condition in LINQ

Posted on 2009-04-02
6
1,228 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

12 Experts available now in Live!

Get 1:1 Help Now