How to: Databind to LINQ Queries
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
Open the solution you created in Creating LINQ to SQL Classes: Using the O/R Designer.
Drag a TextBox control from the Toolbox to Form1.
Drag a Button from the Toolbox to Form1, and change its text property to Run Query.
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;
Press F5.
Type USA in the text box.
Click the Run Query button.
Verify that only customers who have a value of USA in their Country property are displayed. Then try various countries, such as Germany.
See Also
Tasks
Creating LINQ to SQL Classes with the O/R Designer
Walkthrough: Creating LINQ to SQL Classes (O/R Designer)