This documentation is archived and is not being maintained.

How to: Databind to LINQ Queries

Visual Studio 2008

You can use LINQ queries to bind data to controls in your application. To do this, you assign the results of the LINQ query to the data source.

To use LINQ to bind data to a control

  1. Open the solution you created in Creating LINQ to SQL Classes: Using the O/R Designer.

  2. Drag a TextBox control from the Toolbox to Form1.

  3. Drag a Button from the Toolbox to Form1, and change its text property to Run Query.

  4. Double-click the button and add the following code to the Button1_Click event handler:

    var CustomersQuery = from customers in northwindSampleDataContext1.Customers
                          where customers.Country == TextBox1.Text
                          select customers;
    customerBindingSource.DataSource = CustomersQuery;
  5. Press F5.

  6. Type USA in the text box.

  7. Click the Run Query button.

  8. Verify that only customers who have a value of USA in their Country property are displayed. Then try various countries, such as Germany.