Aracılığıyla paylaş


How to: Count, Sum, or Average Data by Using LINQ (Visual Basic)

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. The sample counts, sums, and averages the results by using the Aggregate and Group By clauses. For more information, see Aggregate Clause (Visual Basic) and Group By Clause (Visual Basic).

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 (LINQ to SQL).

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings.

To create a connection to a database

  1. In Visual Studio, open Server Explorer/Database Explorer by clicking Server Explorer/Database Explorer on the View menu.

  2. Right-click Data Connections in Server Explorer/Database Explorer and then click Add Connection.

  3. Specify a valid connection to the Northwind sample database.

To add a project that contains a LINQ to SQL file

  1. In Visual Studio, on the File menu, point to New and then click Project. Select Visual Basic Windows Forms Application as the project type.

  2. On the Project menu, click Add New Item. Select the LINQ to SQL Classes item template.

  3. Name the file northwind.dbml. Click Add. The Object Relational Designer (O/R Designer) is opened for the northwind.dbml file.

To add tables to query to the O/R Designer

  1. 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.

  2. 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 Customer and Order objects 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 the Customer object has an Orders property for all orders related to that customer.

  3. Save your changes and close the designer.

  4. Save your project.

To add code to query the database and display the results

  1. From the Toolbox, drag a DataGridView control onto the default Windows Form for your project, Form1.

  2. Double-click Form1 to add code to the Load event of the form.

  3. When you added tables to the O/R Designer, the designer added a DataContext object for 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 DataContext object for your project is named based on the name of your .dbml file. For this project, the DataContext object is named northwindDataContext.

    You can create an instance of the DataContext in your code and query the tables specified by the O/R Designer.

    Add the following code to the Load event to query the tables that are exposed as properties of your DataContext and count, sum, and average the results. The sample uses the Aggregate clause to query for a single result, and the Group By clause to show an average for grouped results.

    Dim db As New northwindDataContext
    Dim msg = "" 
    
    Dim londonCustomerCount = Aggregate cust In db.Customers _
                              Where cust.City = "London" _
                              Into Count()
    msg &= "Count of London Customers: " & londonCustomerCount & vbCrLf
    
    Dim averageOrderCount = Aggregate cust In db.Customers _
                            Where cust.City = "London" _
                            Into Average(cust.Orders.Count)
    msg &= "Average number of Orders per customer: " & _
           averageOrderCount & vbCrLf
    
    Dim venezuelaTotalOrders = Aggregate cust In db.Customers _
                               Where cust.Country = "Venezuela" _
                               Into Sum(cust.Orders.Count)
    msg &= "Total number of orders from Customers in Venezuela: " & _
           venezuelaTotalOrders & vbCrLf
    
    MsgBox(msg)
    
    Dim averageCustomersByCity = From cust In db.Customers _
                                 Group By cust.City _
                                 Into Average(cust.Orders.Count) _
                                 Order By Average
    
    DataGridView1.DataSource = averageCustomersByCity
    
  4. Press F5 to run your project and view the results.

See Also

Tasks

Walkthrough: Creating LINQ to SQL Classes (O/R Designer)

Concepts

DataContext Methods (O/R Designer)

Reference

Aggregate Clause (Visual Basic)

Group By Clause (Visual Basic)

Other Resources

LINQ in Visual Basic

Queries (Visual Basic)

LINQ to SQL