Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Using LINQ in Visual Basic Express

Applications often use data in SQL databases or XML documents. As a developer, you used to need to learn a secondary language such as SQL or XQuery to work with this data. Language-Integrated Query (LINQ) brings query capabilities into the Visual Basic language itself so that you don't have to learn a completely different query language. Now you can use your existing knowledge of Visual Basic, together with a few additional keywords and concepts, to query SQL databases, ADO.NET datasets, XML documents, and any .NET Framework collection class that implements the IEnumerable interface.

  • Familiar syntax for writing queries

  • Compile-time checking for syntax errors and type safety

  • Improved debugger support

  • IntelliSense support

  • Ability to work directly with XML elements, instead of creating a container XML document, as is required with W3C Document Object Model (DOM)

  • In-memory XML document modification that is powerful yet simpler to use than XPath or XQuery

  • Powerful filtering, ordering, and grouping capabilities

  • Consistent model for working with data in various kinds of data sources and formats

The basic structure of a LINQ query is the same whether you're working with ADO.NET datasets, SQL databases, .NET Framework collections, or XML documents. A query expression starts with a From clause, which is followed by query clauses such as Where and Select. The complete expression is stored in a query variable that can be executed or modified any number of times. Query expression syntax resembles the syntax of SQL. For example, you could use the following syntax to write a LINQ query that returns all students in a students database that have science as their major:

Dim StudentQuery = From student in studentApp.students 
    Where student.Major = "Science" _
    Select student

For more information, see Queries (Visual Basic), and Writing Your First LINQ Query (Visual Basic).

There are three basic stages of a LINQ query. You obtain the data source, define the query expression, and then run the query. For more information, see Writing Your First LINQ Query (Visual Basic).

The term LINQ to Objects refers to the use of LINQ to query in-memory data structures that support IEnumerable. For more information, see LINQ to Objects.

Use LINQ to SQL to access SQL Server and SQL Server Express databases through a strongly-typed object layer that you create by using the O/R Designer.

You can use the O/R Designer to map LINQ to SQL classes to tables in a database and then write LINQ queries to bind data to controls in your application. For example, the following LINQ query binds the results of a LINQ query (all customers from the United States) to a binding source of a DataGridView control.

Dim CustomersQuery = From customers in NorthwindSampleDataContext1.Customers _
    Where customers.Country = "US" _
    Select customers

CustomerBindingSource.DataSource = CustomersQuery

For more information, see LINQ to SQL, O/R Designer Overview, Creating LINQ to SQL Classes: Using the O/R Designer, and Using LINQ to Bind Data to Controls.

NoteNote

The O/R Designer does not currently support SQL Server Compact 3.5 databases. For information about how to obtain SQL Server Express Edition, see the Obtaining SQL Server Express Edition section in How to: Install Sample Databases.

The DataSet is used to bind data to controls in an application. Instead of connecting directly to a database, you can use a DataSet to create an offline cache of data, which can include subsets of several data sources, to be used with an application. When the application is brought online, the database can be updated with the changes in the DataSet.

LINQ to DataSet makes querying over cached data faster and easier than the filtering and sorting methods available to a DataSet. For more information, see LINQ to DataSet.

LINQ to XML enables you to create and modify XML documents easily by using LINQ query expressions instead of having to learn XPath or XQuery. LINQ to XML is a new in-memory API for XML that uses modern programming constructs instead of the W3C DOM. For more information, see Overview of LINQ to XML in Visual Basic, Including XML Directly in Your Code: Using XML Literals, and Language-Integrated Axes in Visual Basic (LINQ to XML).

Show:
© 2015 Microsoft