You have seen how ADO.NET Entities can be queried using text queries based on an SQL “dialect” specifically and intentionally defined for querying entities. However, entities are a typed representation of conceptual information, and it would be great to write queries using a fully typed approach. As you saw at the end of Chapter 5, “LINQ to ADO.NET,” we can leverage LINQ to Entities to query ADO.NET Entities using a LINQ query approach. Listing A-16 offers a quick review of these capabilities, showing a possible way to translate the query used in Listing A-15 into LINQ to Entities.
Listing A-16: A LINQ to Entities query to extract all the customers who placed orders in the last two years
using (NorthwindEntities db = new NorthwindEntities()) { DateTime referenceDate = DateTime.Now.AddYears(-10); var customers = (from o in db.Orders where o.OrderDate > referenceDate select o.Customer).Distinct(); foreach (var c in customers) { Console.WriteLine(c.Display()); } }
The LINQ approach enables you to write fully typed code, as you can see from the typed DateTime filter.