Home | Table Design |
Distinct
This sample uses Distinct to remove duplicate Unit
SQL:
SELECT DISTINCT Unit
FROM CustOrdeItem
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = (from oi in db.CustOrdeItems
select oi.Unit).Distinct();
Lambda:
var result = db.CustOrdeItems.Select(oi => oi.Unit).Distinct();
foreach (var oi in result)
Response.Output.WriteLine("{0}<br> ", oi);
Result:
KG
Pcs
Union
This sample uses Union to create one sequence that contains the unique values from both arrays.
SQL:
SELECT Name
FROM Customer
UNION
SELECT Name
FROM Supplier
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = (from c in db.Customers
select c.Name)
.Union
(from c in db.Suppliers
select c.Name);
Lambda:
var result = db.Customers.Select(c => c.Name)
.Union(db.Suppliers.Select(c => c.Name));
foreach (var o in result)
Response.Output.WriteLine("{0}<br> ", o);
Result:
Aarti Chapani
Ayush Patel
Rachit Rokad
Nenshi
Parul Makadiya
Poonam Makati
Vimal Patel
Yuti Vachani
Intersect
This sample uses Intersect to create one sequence that contains the common Name shared by both Customer and Supplier.
SQL:
SELECT Name
FROM Customer
INTERSECT
SELECT name
FROM Supplier
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = (from c in db.Customers
select c.Name)
.Intersect
(from c in db.Suppliers
select c.Name);
Lambda:
var result = db.Customers.Select(c => c.Name)
.Intersect(db.Suppliers.Select(c => c.Name));
foreach (var o in result)
Response.Output.WriteLine("{0}<br> ", o);
Result:
Rachit Rokad
Except
This sample uses Except to Name that contains the values from Customer that are not also in Supplier.
SQL:
SELECT DISTINCT Name
FROM Customer
WHERE Name NOT IN(SELECT Name FROM Supplier)
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = (from c in db.Customers
select c.Name).
Except(from c in db.Suppliers
select c.Name);
Lambda:
var result = db.Customers.Select(c => c.Name)
.Except(db.Suppliers.Select(c => c.Name));
foreach (var o in result)
Response.Output.WriteLine("{0}<br> ", o);
Result:
Ayush Patel
Nenshi
Poonam Makati
Yuti Vachani
==============================
Shradhdha Zalavadiya |Senior Software Engineer
WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in
No comments:
Post a Comment