The first and most obvious application of LINQ is for querying an external relational database. LINQ to SQL is a component of the LINQ Project that provides the capability to query a relational database, offering you an object model based on available entities. In other words, you can define a set of objects that represents a thin abstraction layer over the relational data, and you can query this object model using LINQ queries that are converted into corresponding SQL queries by the LINQ to SQL component.
In LINQ to SQL, we can write a simple query like the following:
This query is converted into an SQL query that is sent to the relational database:
|
Important |
The SQL queries generated by LINQ that we show in this chapter are only illustrative. Microsoft reserves the right to change the SQL that is generated by LINQ, and at times we use simplified queries in the text. Thus, you should not rely on the SQL that is shown.
|
At this point, you might be asking a few questions. First, how can the LINQ query be written using object names that are validated by the compiler? Second, when is the SQL query generated from the LINQ query? Third, when is the SQL query executed? To understand the answers to these questions, you need to understand the entity model in LINQ to SQL and then the deferred query evaluation.