Linq to sql
Select all
table data than use that query
========================================================================
DataClassesDataContext db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs select emp;
output-
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
|
3
|
mukesh
|
p003
|
jhasi
|
|
4
|
yogi
|
p004
|
eta
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
GridView1.DataSource = d;
GridView1.DataBind();
For particular record we use
where clause
Like this-
DataClassesDataContext
db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs where emp.ctmr_name == "sanjeev" select
emp;
GridView1.DataSource = d;
GridView1.DataBind();
Output
--
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
Or another way also that
DataClassesDataContext
db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name == "sanjana" select
emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
name
|
age
|
sex
|
id
|
Photo
|
amount
|
|
sanjana
|
6
|
female
|
2
|
/images/Snapshot_20120318_11.jpg
|
324323.00
|
DataClassesDataContext db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs select new
{ emp.ctmr_name,emp.ctmr_city};
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
Out put—
|
ctmr_name
|
ctmr_city
|
|
rajeev
|
Delhi
|
|
raju
|
Talwar
|
|
mukesh
|
Jhasi
|
|
yogi
|
Eta
|
|
sanjeev
|
bulandshahar
|
If we want to show only 2
recort than query
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs select emp).Take(2);
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
Use of orderby –
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs orderby emp.ctmr_id select emp);
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
|
3
|
mukesh
|
p003
|
jhasi
|
|
4
|
yogi
|
p004
|
eta
|
=====================================================================================
Use of
desending in with odrer by
DataClassesDataContext db = new DataClassesDataContext();
var d = from emp in db.ctmrs orderby
emp.ctmr_id descending select
emp;
GridView1.DataSource = d;
GridView1.DataBind();
|
5
|
sanjeev
|
p005
|
bulandshahar
|
=============================================================================
For only
single id who is max than used
var d = from emp in db.ctmrs orderby emp.ctmr_id descending
select emp.ctmr_id;
this query
out put ===
|
id
|
|
5
|
|
4
|
|
3
|
|
2
|
|
1
|
For asending order
we use this query
var d = from emp in db.ctmrs orderby emp.ctmr_id ascending
select emp.ctmr_id;
output---
|
Item
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
Use of like operator in linq
We find out which name star
with s and only one
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs where emp.ctmr_name.StartsWith("s") select
emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
Bulandshahar
|
Taking two
highest id in table
DataClassesDataContext db = new DataClassesDataContext();
var d = (from emp in db.ctmrs orderby emp.ctmr_id descending
select emp).Take(2);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
|
4
|
yogi
|
p004
|
Eta
|
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name.EndsWith("c")
select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name.Contains("e")
select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
Use of distinct
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs select
emp).Distinct();
GridView1.DataSource = d;
GridView1.DataBind();Select all
table data than use that query
========================================================================
DataClassesDataContext db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs select emp;
output-
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
|
3
|
mukesh
|
p003
|
jhasi
|
|
4
|
yogi
|
p004
|
eta
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
GridView1.DataSource = d;
GridView1.DataBind();
For particular record we use
where clause
Like this-
DataClassesDataContext
db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs where emp.ctmr_name == "sanjeev" select
emp;
GridView1.DataSource = d;
GridView1.DataBind();
Output
--
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
Or another way also that
DataClassesDataContext
db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name == "sanjana" select
emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
name
|
age
|
sex
|
id
|
Photo
|
amount
|
|
sanjana
|
6
|
female
|
2
|
/images/Snapshot_20120318_11.jpg
|
324323.00
|
DataClassesDataContext db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs select new
{ emp.ctmr_name,emp.ctmr_city};
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
Out put—
|
ctmr_name
|
ctmr_city
|
|
rajeev
|
Delhi
|
|
raju
|
Talwar
|
|
mukesh
|
Jhasi
|
|
yogi
|
Eta
|
|
sanjeev
|
bulandshahar
|
If we want to show only 2
recort than query
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs select emp).Take(2);
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
Use of orderby –
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs orderby emp.ctmr_id select emp);
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
|
3
|
mukesh
|
p003
|
jhasi
|
|
4
|
yogi
|
p004
|
eta
|
=====================================================================================
Use of
desending in with odrer by
DataClassesDataContext db = new DataClassesDataContext();
var d = from emp in db.ctmrs orderby
emp.ctmr_id descending select
emp;
GridView1.DataSource = d;
GridView1.DataBind();
|
5
|
sanjeev
|
p005
|
bulandshahar
|
=============================================================================
For only
single id who is max than used
var d = from emp in db.ctmrs orderby emp.ctmr_id descending
select emp.ctmr_id;
this query
out put ===
|
id
|
|
5
|
|
4
|
|
3
|
|
2
|
|
1
|
For asending order
we use this query
var d = from emp in db.ctmrs orderby emp.ctmr_id ascending
select emp.ctmr_id;
output---
|
Item
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
Use of like operator in linq
We find out which name star
with s and only one
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs where emp.ctmr_name.StartsWith("s") select
emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
Bulandshahar
|
Taking two
highest id in table
DataClassesDataContext db = new DataClassesDataContext();
var d = (from emp in db.ctmrs orderby emp.ctmr_id descending
select emp).Take(2);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
|
4
|
yogi
|
p004
|
Eta
|
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name.EndsWith("c")
select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name.Contains("e")
select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
Use of distinct
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs select
emp).Distinct();
GridView1.DataSource = d;
GridView1.DataBind();Select all
table data than use that query
========================================================================
DataClassesDataContext db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs select emp;
output-
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
|
3
|
mukesh
|
p003
|
jhasi
|
|
4
|
yogi
|
p004
|
eta
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
GridView1.DataSource = d;
GridView1.DataBind();
For particular record we use
where clause
Like this-
DataClassesDataContext
db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs where emp.ctmr_name == "sanjeev" select
emp;
GridView1.DataSource = d;
GridView1.DataBind();
Output
--
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
Or another way also that
DataClassesDataContext
db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name == "sanjana" select
emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
name
|
age
|
sex
|
id
|
Photo
|
amount
|
|
sanjana
|
6
|
female
|
2
|
/images/Snapshot_20120318_11.jpg
|
324323.00
|
DataClassesDataContext db = new DataClassesDataContext();
var d =
from emp in
db.ctmrs select new
{ emp.ctmr_name,emp.ctmr_city};
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
Out put—
|
ctmr_name
|
ctmr_city
|
|
rajeev
|
Delhi
|
|
raju
|
Talwar
|
|
mukesh
|
Jhasi
|
|
yogi
|
Eta
|
|
sanjeev
|
bulandshahar
|
If we want to show only 2
recort than query
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs select emp).Take(2);
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
Use of orderby –
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs orderby emp.ctmr_id select emp);
// var d =
(from emp in db.where emp.name.Contains("e") select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
1
|
rajeev
|
p001
|
delhi
|
|
2
|
raju
|
p002
|
talwar
|
|
3
|
mukesh
|
p003
|
jhasi
|
|
4
|
yogi
|
p004
|
eta
|
=====================================================================================
Use of
desending in with odrer by
DataClassesDataContext db = new DataClassesDataContext();
var d = from emp in db.ctmrs orderby
emp.ctmr_id descending select
emp;
GridView1.DataSource = d;
GridView1.DataBind();
|
5
|
sanjeev
|
p005
|
bulandshahar
|
=============================================================================
For only
single id who is max than used
var d = from emp in db.ctmrs orderby emp.ctmr_id descending
select emp.ctmr_id;
this query
out put ===
|
id
|
|
5
|
|
4
|
|
3
|
|
2
|
|
1
|
For asending order
we use this query
var d = from emp in db.ctmrs orderby emp.ctmr_id ascending
select emp.ctmr_id;
output---
|
Item
|
|
1
|
|
2
|
|
3
|
|
4
|
|
5
|
Use of like operator in linq
We find out which name star
with s and only one
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs where emp.ctmr_name.StartsWith("s") select
emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
Bulandshahar
|
Taking two
highest id in table
DataClassesDataContext db = new DataClassesDataContext();
var d = (from emp in db.ctmrs orderby emp.ctmr_id descending
select emp).Take(2);
GridView1.DataSource = d;
GridView1.DataBind();
|
ctmr_id
|
ctmr_name
|
productid
|
ctmr_city
|
|
5
|
sanjeev
|
p005
|
bulandshahar
|
|
4
|
yogi
|
p004
|
Eta
|
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name.EndsWith("c")
select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.loves where emp.name.Contains("e")
select emp).Take(1);
GridView1.DataSource = d;
GridView1.DataBind();
Use of distinct
DataClassesDataContext db = new DataClassesDataContext();
var d =
(from emp in
db.ctmrs select
emp).Distinct();
GridView1.DataSource = d;
GridView1.DataBind();
No comments:
Post a Comment