| title | How to: Sort Query Results by Using LINQ (Visual Basic) | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| ms.custom | |||||||||
| ms.date | 07/20/2015 | ||||||||
| ms.prod | .net | ||||||||
| ms.reviewer | |||||||||
| ms.suite | |||||||||
| ms.technology |
|
||||||||
| ms.topic | article | ||||||||
| helpviewer_keywords |
|
||||||||
| ms.assetid | 07a4584d-9fd8-4a1d-b7d9-ccf2efa5c84e | ||||||||
| caps.latest.revision | 5 | ||||||||
| author | dotnet-bot | ||||||||
| ms.author | dotnetcontent |
Language-Integrated Query (LINQ) makes it easy to access database information and execute queries.
The following example shows how to create a new application that performs queries against a SQL Server database and sorts the results by multiple fields by using the Order By clause. The sort order for each field can be ascending order or descending order. For more information, see Order By Clause.
The examples in this topic use the Northwind sample database. If you do not have the Northwind sample database on your development computer, you can download it from the Microsoft Download Center Web site. For instructions, see Downloading Sample Databases.
[!INCLUDEnote_settings_general]
-
In Visual Studio, open Server Explorer/Database Explorer by clicking Server Explorer/Database Explorer on the View menu.
-
Right-click Data Connections in Server Explorer/Database Explorer and then click Add Connection.
-
Specify a valid connection to the Northwind sample database.
-
In Visual Studio, on the File menu, point to New and then click Project. Select Visual Basic Windows Forms Application as the project type.
-
On the Project menu, click Add New Item. Select the LINQ to SQL Classes item template.
-
Name the file
northwind.dbml. Click Add. The Object Relational Designer (O/R Designer) is opened for the northwind.dbml file.
-
In Server Explorer/Database Explorer, expand the connection to the Northwind database. Expand the Tables folder.
If you have closed the O/R Designer, you can reopen it by double-clicking the northwind.dbml file that you added earlier.
-
Click the Customers table and drag it to the left pane of the designer. Click the Orders table and drag it to the left pane of the designer.
The designer creates new
CustomerandOrderobjects for your project. Notice that the designer automatically detects relationships between the tables and creates child properties for related objects. For example, IntelliSense will show that theCustomerobject has anOrdersproperty for all orders related to that customer. -
Save your changes and close the designer.
-
Save your project.
-
From the Toolbox, drag a xref:System.Windows.Forms.DataGridView control onto the default Windows Form for your project, Form1.
-
Double-click Form1 to add code to the
Loadevent of the form. -
When you added tables to the O/R Designer, the designer added a xref:System.Data.Linq.DataContext object to your project. This object contains the code that you must have to access those tables, and to access individual objects and collections for each table. The xref:System.Data.Linq.DataContext object for your project is named based on the name of your .dbml file. For this project, the xref:System.Data.Linq.DataContext object is named
northwindDataContext.You can create an instance of the xref:System.Data.Linq.DataContext in your code and query the tables specified by the O/R Designer.
Add the following code to the
Loadevent to query the tables that are exposed as properties of your data context and sort the results. The query sorts the results by the number of customer orders, in descending order. Customers that have the same number of orders are ordered by company name in ascending order (the default).[!code-vbVbLINQToSQLHowTos#10]
-
Press F5 to run your project and view the results.