Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to get top records using where condition in LINQ

Posted on 2009-04-02
6
Medium Priority
?
1,245 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 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

971 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