Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
LINQ To SQL: First operator
WhatsApp
Vijai Anand Ramalingam
13y
3.9
k
0
0
25
Blog
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();
Up Next
LINQ To SQL: Order by operator
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
Membership not found