Procedura: ordinare gli elementi in una sequenza (LINQ to SQL)
Utilizzare l'operatore OrderBy per ordinare una sequenza in base a una o più chiavi.
![]() |
---|
LINQ to SQL è progettato per supportare l'ordinamento in base a semplici tipi primitivi, ad esempio string, int e così via.Non supporta invece l'ordinamento per classi multivalore complesse, ad esempio i tipi anonimi.Non supporta inoltre i tipi di dati byte. |
Esempio
Nell'esempio seguente vengono ordinati Employees in base alla data di assunzione.
Dim hireQuery = _
From emp In db.Employees _
Select emp _
Order By emp.HireDate
For Each empObj As Employee In hireQuery
Console.WriteLine("EmpID = {0}, Date Hired = {1}", _
empObj.EmployeeID, empObj.HireDate)
Next
IOrderedQueryable<Employee> hireQuery =
from emp in db.Employees
orderby emp.HireDate
select emp;
foreach (Employee empObj in hireQuery)
{
Console.WriteLine("EmpID = {0}, Date Hired = {1}",
empObj.EmployeeID, empObj.HireDate);
}
Nell'esempio seguente viene utilizzato where per l'ordinamento di Orders spediti a London in base al costo di spedizione.
Dim freightQuery = _
From ord In db.Orders _
Where ord.ShipCity = "London" _
Select ord _
Order By ord.Freight
For Each ordObj In freightQuery
Console.WriteLine("Order ID = {0}, Freight = {1}", _
ordObj.OrderID, ordObj.Freight)
Next
IOrderedQueryable<Order> freightQuery =
from ord in db.Orders
where ord.ShipCity == "London"
orderby ord.Freight
select ord;
foreach (Order ordObj in freightQuery)
{
Console.WriteLine("Order ID = {0}, Freight = {1}",
ordObj.OrderID, ordObj.Freight);
}
Nell'esempio seguente vengono ordinati Products in base al prezzo unitario dal più alto al più basso.
Dim priceQuery = _
From prod In db.Products _
Select prod _
Order By prod.UnitPrice Descending
For Each prodObj In priceQuery
Console.WriteLine("Product ID = {0}, Unit Price = {1}", _
prodObj.ProductID, prodObj.UnitPrice)
Next
IOrderedQueryable<Product> priceQuery =
from prod in db.Products
orderby prod.UnitPrice descending
select prod;
foreach (Product prodObj in priceQuery)
{
Console.WriteLine("Product ID = {0}, Unit Price = {1}",
prodObj.ProductID, prodObj.UnitPrice);
}
Nell'esempio seguente viene utilizzato il tipo OrderBy composto per ordinare Customers in base alla città, quindi al nome del contatto.
Dim custQuery = _
From cust In db.Customers _
Select cust _
Order By cust.City, cust.ContactName
For Each custObj In custQuery
Console.WriteLine("City = {0}, Name = {1}", custObj.City, _
custObj.ContactName)
Next
IOrderedQueryable<Customer> custQuery =
from cust in db.Customers
orderby cust.City, cust.ContactName
select cust;
foreach (Customer custObj in custQuery)
{
Console.WriteLine("City = {0}, Name = {1}", custObj.City,
custObj.ContactName);
}
Nell'esempio seguente vengono ordinati gli ordini da EmployeeID 1 in base al paese di spedizione, quindi in base al costo di spedizione dal più alto al più basso.
Dim ordQuery = _
From ord In db.Orders _
Where CInt(ord.EmployeeID.Value) = 1 _
Select ord _
Order By ord.ShipCountry, ord.Freight Descending
For Each ordObj In ordQuery
Console.WriteLine("Country = {0}, Freight = {1}", _
ordObj.ShipCountry, ordObj.Freight)
Next
IOrderedQueryable<Order> ordQuery =
from ord in db.Orders
where ord.EmployeeID == 1
orderby ord.ShipCountry, ord.Freight descending
select ord;
foreach (Order ordObj in ordQuery)
{
Console.WriteLine("Country = {0}, Freight = {1}",
ordObj.ShipCountry, ordObj.Freight);
}
Nell'esempio seguente vengono combinati gli operatori OrderBy, Max e GroupBy per trovare Products con il prezzo unitario più alto in ciascuna categoria, quindi per ordinare il gruppo in base all'ID della categoria.
Dim highPriceQuery = From prod In db.Products _
Group prod By prod.CategoryID Into grouping = Group _
Order By CategoryID _
Select CategoryID, _
MostExpensiveProducts = _
From prod2 In grouping _
Where prod2.UnitPrice = _
grouping.Max(Function(p3) p3.UnitPrice)
For Each prodObj In highPriceQuery
Console.WriteLine(prodObj.CategoryID)
For Each listing In prodObj.MostExpensiveProducts
Console.WriteLine(listing.ProductName)
Next
Next
var highPriceQuery =
from prod in db.Products
group prod by prod.CategoryID into grouping
orderby grouping.Key
select new
{
grouping.Key,
MostExpensiveProducts =
from prod2 in grouping
where prod2.UnitPrice == grouping.Max(p3 => p3.UnitPrice)
select prod2
};
foreach (var prodObj in highPriceQuery)
{
Console.WriteLine(prodObj.Key);
foreach (var listing in prodObj.MostExpensiveProducts)
{
Console.WriteLine(listing.ProductName);
}
}
Se si esegue la query precedente sul database di esempio Northwind, i risultati saranno simili ai seguenti:
1
Côte de Blaye
2
Vegie-spread
3
Sir Rodney's Marmalade
4
Raclette Courdavault
5
Gnocchi di nonna Alice
6
Thüringer Rostbratwurst
7
Manjimup Dried Apples
8
Carnarvon Tigers
Vedere anche
Concetti
Download dei database di esempio (LINQ to SQL)