LINQ to Entities
One of the exciting innovations introduced in Visual Studio 2008 is Language Integrated Query or LINQ, which surfaces as extensions to C# 3.0 and Visual Basic 9.0. LINQ to Entities is a variation on LINQ that understands how to query entities. With LINQ to Entities, although you lose the flexibility of building dynamic queries, you gain IntelliSense and many other benefits of LINQ.
|With LINQ to Entities, although you lose the flexibility of building dynamic queries, you gain simplicity with IntelliSense and many other benefits of LINQ.|
This LINQ to Entities query returns a System.Linq.IQueryable of Order entities. An iQueryable is a collection that both allows enumeration through the collection and also lets you make queries against it. However, an iQueryable doesn't expose any of the other typical methods of a collection for manipulating the list of objects. Once you have the results, you can build a query against them as well:
Dim orders = _
From o In NWEntities.Orders _
Where o.Customers.Country = "Spain" _
If you type this into Visual Studio 2008 yourself, you will see that IntelliSense helps you construct the query as in Figure 7
|Figure 7: LINQ to Entities is able to leverage IntelliSense to assist you in building queries.|
The following LINQ to Entities example queries into a collection property (the Orders attached to a Customer) that leverages lambda expressions
. Lambda expressions in Visual Basic 9.0 are quite different than in C# 3.0. This query grabs only those customers who have orders placed after July 1, 2007:
Querying and Updating Across Relationships
Dim q = From cust In nwentities.Customers _
Where (cust.Orders.Any(Function(o) _
o.OrderDate > New DateTime(2007, 7, 1)))
So far, most of these introductory examples have focused on a single entity type. However, in the first LINQ to Entities example, the query traversed from Orders to related Customers data by filtering on the Customers.Country
value of a particular order. This is possible because of the associations in the schema—it is not necessary to re-define them in your queries. Whether you are getting the benefit of IntelliSense in LINQ to Entities to help you navigate through these relationships, or using Entity SQL, the ADO.NET Entity Framework will orchestrate retrieving and updating the related data.
Thanks to the Entity Objects, you can access related data as properties of the entities. The associations and navigation properties in the conceptual layer provide relationships in both directions—parent to child and child to parent.
Here are two Entity SQL queries that traverse from Orders to Customers. The first returns fields from orders and customers, the second filters on customer data:
Dim ord = NWEntities.CreateQuery(Of DbDataRecord)_
("SELECT o.Customers.CompanyName, " & _
"o.OrderDate,o.Freight FROM orders as o " & _
"WHERE o.ShipCity='Sao Paulo'")
Dim ord = NWEntities.CreateQuery(Of Orders) _
("SELECT VALUE o FROM orders as o " & _
While you can also go the other way, this requires a little more work because you will be drilling from Customers into a collection (of Orders in this case). You can traverse collections in Entity SQL only through SELECT/FROM
expressions, so you need a sub-query to get at the Customers.
This Entity SQL example of querying into a child collection looks very similar to T-SQL:
Dim custs = NWEntities.CreateQuery(Of Customers) _
("SELECT VALUE c FROM Customers as c " & _
"WHERE c.CustomerID IN " & _
"(SELECT VALUE o.Customers.CustomerID " & _
"FROM Orders as o " & _
"WHERE o.OrderDate > " & _
The same query in LINQ to Entities looks like this (note that this C# example leverages lambda expressions, which makes it simple to construct the relevant query):
var q = from customer in northwind.Customers
o=>o.OrderDate > new DateTime(2007,7,1))
While Microsoft is building providers for SQL Server, there are many other vendors busy creating providers so that you can access their data through the ADO.NET Entity Framework as well. At TechEd 2007, IBM spoke about and demonstrated their prototype ADO.NET Entity Framework provider and discussed an Informix provider that they are working on. Earlier this year, MySQL demonstrated a prototype of a MySQL provider at their annual conference. An interesting point to remember is that a provider does not necessarily have to be a database. It can be any type of data storage, such as XML files.
While the ADO.NET Entity Framework will not be included in the Visual Studio 2008 RTM (expected by the end of 2007); it will be released in the first half of 2008.
There will be either a CTP (Community Technical Preview) or a beta of ADO.NET Entity Framework released along with any Visual Studio 2008 releases, including the Visual Studio 2008 RTM.
This article has provided a basic overview and introduction to ADO.NET Entity Framework, and it only scratches the surface. The ADO.NET Team has been writing a number of in-depth posts on their blog about various aspects of ADO.NET Entity Framework. There is also an enormous amount to learn in the forums. As more people explore this technology, you can expect to see articles that dig much deeper to explore building complex, real-world applications using ADO.NET Entity Framework.