Solved

how to get top records using where condition in LINQ

Posted on 2009-04-02
6
1,241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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