Home | Table Design |
OrderBy - Simple 1
This sample uses orderby to sort a Item of order.
SQL:
SELECT CustOIId, CustOID, Item, Qty, Unit, Price
FROM CustOrdeItem
ORDER BY Item
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from oi in db.CustOrdeItems
orderby oi.Item
select oi;
Lambda:
var result = db.CustOrdeItems.OrderBy(oi => oi.Item);
foreach (var oi in result)
Response.Output.WriteLine("{0}<br> ", oi.Item);
Result:
Bag
Colgate
Grocery
iron
Plastic chair
Shoes
OrderBy - Simple 2
This sample uses orderby to sort a list of Item by length.
SQL:
SELECT CustOIId, CustOID, Item, Qty, Unit, Price
FROM CustOrdeItem
ORDER BY LEN(Item)
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from oi in db.CustOrdeItems
orderby oi.Item.Length
select oi;
Lambda:
var result = db.CustOrdeItems.OrderBy(oi => oi.Item.Length);
foreach (var oi in result)
Response.Output.WriteLine("{0}<br> ", oi.Item);
Result:
Bag
iron
Shoes
Colgate
Grocery
Plastic chair
ThenBy
This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by Student and then by Age sort of the words in an array.
SQL:
SELECT CustID, Name, Sex, Education, IsStudent, Age, ContactNo
FROM Customer
ORDER BY IsStudent, Age
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = db.Customers.OrderBy(c => c.IsStudent).ThenBy(c => c.Age);
foreach (var oi in result)
Response.Output.WriteLine("{0}<br> ", oi.Name);
Result:
Nenshi
Rachit Rokad
Ayush Patel
Yuti Vachani
Poonam Makati
ThenByDescending
This sample uses an OrderBy and a ThenBy clause with a comparer to sort first by student and then by Age descending.
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = db.Customers.OrderBy(c => c.IsStudent).ThenByDescending(c=> c.Age);
foreach (var oi in result)
Response.Output.WriteLine("{0}<br> ", oi.Name);
Result:
Rachit Rokad
Nenshi
Poonam Makati
Yuti Vachani
Ayush Patel
OrderByDescending - Simple 1
This sample uses orderby and descending to sort a list of item.
SQL:
SELECT CustOIId, CustOID, Item, Qty, Unit, Price
FROM CustOrdeItem
ORDER BY Item DESC
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from oi in db.CustOrdeItems
orderby oi.Item descending
select oi;
Lambda:
var result = db.CustOrdeItems.OrderByDescending(oi => oi.Item);
foreach (var oi in result)
Response.Output.WriteLine("{0}<br> ", oi.Item);
Result:
Shoes
Plastic chair
iron
Grocery
Colgate
Bag
ThenBy - Simple
This sample uses a compound orderby to sort a list of Items, first by length of their Items, and then alphabetically by the name itself.
SQL:
SELECT CustOIId, CustOID, Item, Qty, Unit, Price
FROM CustOrdeItem
ORDER BY Item, LEN(Item)
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from oi in db.CustOrdeItems
orderby oi.Item.Length, oi.Item
select oi;
Lambda:
var result = db.CustOrdeItems.OrderBy(oi => oi.Item.Length).OrderBy(oi => oi.Item);
foreach (var oi in result)
Response.Output.WriteLine("{0}<br> ", oi.Item);
Result:
Bag
iron
Shoes
Colgate
Grocery
Plastic chair
ThenByDescending - Simple
This sample uses a compound orderby to sort a list of items, first by length of their Items, and then alphabetically by the name itself, from highest to lowest.
SQL:
SELECT CustOIId, CustOID, Item, Qty, Unit, Price
FROM CustOrdeItem
ORDER BY LEN(Item), Item DESC
DataClassesDataContext db = new DataClassesDataContext();
LINQ:
var result = from oi in db.CustOrdeItems
orderby oi.Item.Length, oi.Item descending
select oi;
foreach (var oi in result)
Response.Output.WriteLine("{0}<br> ", oi.Item);
Result:
Bag
iron
Shoes
Grocery
Colgate
Plastic chair
==============================
Shradhdha Zalavadiya |Senior Software Engineer
WebMingle Technology
Accelerated by knowledge. Driven by values.
www.webMingle.in
No comments:
Post a Comment