TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
LINQ To SQL: First operator
Vijai Anand Ramalingam
Jun 20, 2011
3.7
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
In this blog we will be seeing how to Return the First Element in a Sequence
EmpDetails Table:
Code:
To Return the First Element in a Sequence.
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
LINQ
{
class
Program
{
static
void
Main(
string
[] args)
{
DataClassesDataContext
dc =
new
DataClassesDataContext
();
EmpDetail
empQuery = (
from
emp
in
dc.EmpDetails
where
emp.Dept ==
"MOSS"
select
emp).First();
Console
.WriteLine(
"ID :{0}, Name :{1}, Location :{2}, Department :{3}"
, empQuery.ID, empQuery.Name, empQuery.Location, empQuery.Dept);
Console
.ReadLine();
}
}
}
Output:
ID :1, Name :Vijai, Location :Bangalore, Department :MOSS
Alternate method:
DataClassesDataContext
dc =
new
DataClassesDataContext
();
EmpDetail
empQuery = dc.EmpDetails.First();
Console
.WriteLine(
"ID :{0}, Name :{1}, Location :{2}, Department :{3}"
, empQuery.ID, empQuery.Name, empQuery.Location, empQuery.Dept);
Console
.ReadLine();
Next Recommended Reading
LINQ To SQL: Order by operator