Using LINQ in C#
It is very common for applications to use data in SQL databases or XML documents. Traditionally, developers had to learn a primary programming language, such as C#, and a secondary language, such as SQL or XQuery. Language-Integrated Query (LINQ) brings query capabilities into the C# language itself. Now, instead of learning a separate query language, you can use your knowledge of C#, together with some additional keywords and concepts, to query SQL databases, ADO.NET datasets, XML documents, and any .NET collection class that implements the IEnumerable interface.
Familiar syntax for writing queries.
Compile-time checking for syntax errors and type safety.
Improved debugger support.
Ability to work directly with XML elements instead of creating a container XML document, as required with W3C 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 across various kinds of data sources and formats.
Following are some new language structures that are added to C# 3.0 to support LINQ:
implicitly typed variables
A variable, specified with the modifier var, which enables the compiler to infer the type of the variable. For more information, see Implicitly Typed Local Variables (C# Programming Guide) and C# 3.0 Features That Support LINQ.
Enables the initialization of objects without explicitly calling a constructor for the object. For more information, see Object and Collection Initializers (C# Programming Guide) and C# 3.0 Features That Support LINQ.
Enables the compiler to create objects without requiring that you specify a named data type. The type name is only available to the compiler. For more information, see Anonymous Types (C# Programming Guide) and C# 3.0 Features That Support LINQ.
Enables the extension of any existing type by associating static methods to the type. For more information, see C# 3.0 Features That Support LINQ.
An inline expression or statement block that can be used wherever a delegate type is expected.
Whether you are working with ADO.NET datasets, SQL databases, .NET collections, or XML documents, the basic structure of a LINQ query expression is the same. A query expression is starts with a from clause, followed by query clauses such as where, orderby, select, and so on. The complete expression is stored in a query variable which can be executed or modified any number of times. Query expression syntax resembles the syntax of SQL. For example, you could write a LINQ query that returns all students in a students database that have science as their major, by using the following syntax:
IEnumerable<Student> studentQuery =
from student in studentApp.students
where student.Major == "Science"
The phrase, LINQ to Objects, refers to the use of LINQ to query in-memory data collections such as the classes in the System.Collections and System.Collections.Generic namespaces. These classes include ArrayList, List<T>, Dictionary<K,T>, and so on. You can query arrays because they implicitly support IEnumerable<T>. For more information, see LINQ to Objects.
You can even import a text file into enumerable data structures and run queries to filter or sort its contents. For an example, see How to: Count Occurrences of a Word in a String (LINQ).
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.
var 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, Walkthrough: Creating LINQ to SQL Classes (O/R Designer), and How to: Add LINQ to SQL Classes to a Project (O/R Designer).
The O/R Designer currently does not 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. Rather than connecting directly to the database, the DataSet enables an application to use off-line (cached) data, or subsets of several data sources. When the application is brought online, the changes in the DataSet can be updated in the database.
LINQ to XML enables you to create and modify XML documents easily by using LINQ query expressions instead of XPath or XQuery. LINQ to XML is a new in-memory XML programming API that uses modern programming constructs instead of the W3C Document Object Model (DOM). For more information, see LINQ to XML vs. DOM, LINQ to XML, and LINQ to XML Classes Overview.