Querying ADO.NET Entities with LINQ


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

image from book
  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());     } } 
image from book

The LINQ approach enables you to write fully typed code, as you can see from the typed DateTime filter.




Introducing Microsoft LINQ
Introducing MicrosoftВ® LINQ
ISBN: 0735623910
EAN: 2147483647
Year: 2007
Pages: 78

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net