Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to get top records using where condition in LINQ

Posted on 2009-04-02
6
Medium Priority
?
1,243 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 1500 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
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 
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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

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…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

660 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