And in case youre wondering about linq to xml that

Info icon This preview shows pages 592–595. Sign up to view the full content.

View Full Document Right Arrow Icon
§ And in case you’re wondering about LINQ to XML, that doesn’t help here. It lets you use LINQ-capable languages like C# or VB.NET to write LINQ queries to look in an XML document. It doesn’t let you put LINQ queries in an XML document. 568 | Chapter 14: Databases
Image of page 592

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Of course, string-based queries have a massive downside compared to LINQ: the C# compiler cannot offer any help as it doesn’t understand ESQL. If your ESQL strings are badly formed, you only get to find that out at runtime. And even if your ESQL is syntactically correct, C# does not understand the relationship between it and your code—whereas with a LINQ to Entities query C# can detect things such as type mis- matches, it won’t spot when your ESQL gets out of sync with the code that uses the results. Besides the inherent benefits and disadvantages of a string-based query, there’s also the fact that ESQL is, in effect, the native query language for the EF. This means there are a few EF features that can be accessed only through ESQL, although they’re all some- what arcane. For example, an ESQL query can navigate associations between entities even if you’ve neglected to define navigation properties to represent those associations. Example 14-12 shows a simple example that illustrates the basic use of ESQL. Example 14-12. Querying with ESQL using (var dbContext = new AdventureWorksLT2008Entities()) { DateTime orderDate = new DateTime(2004, 6, 1); var query = dbContext.CreateQuery<SalesOrderHeader>("SELECT VALUE o " + "FROM AdventureWorksLT2008Entities.SalesOrderHeaders AS o " + "WHERE o.OrderDate = @orderDate", new ObjectParameter("orderDate", orderDate)); foreach (var order in query) { Console.WriteLine(order.TotalDue); } } This has the same effect as Example 14-3 , but using ESQL in place of a LINQ query. While this looks similar to a typical SQL query, the VALUE keyword is specific to ESQL. We use this to indicate that we don’t want the usual column-like behavior of SQL. You can write a more traditional-looking query in ESQL, such as: SELECT o.TotalDue, o.OrderDate FROM AdventureWorksLT2008Entities.SalesOrderHeaders AS o WHERE o.OrderDate = @orderDate This asks for specific columns from the entity rather than the whole entity. This is legal ESQL, but it would fail at runtime in the context of Example 14-12 . That example creates the query with a call to CreateQuery<SalesOrderHeader> on the object context. The generic type argument to CreateQuery SalesOrderHeader here—indicates the type of result we’re expecting from the query, but this modified query clearly returns some- thing other than a SalesOrderHeader . It returns a couple of columns from each matching entity. When you build a query like this, you get back objects that implement IDataRecord —a general-purpose interface used across all of ADO.NET to represent a record (such as a table row) whose columns might not be known until runtime. (This Queries | 569
Image of page 593
is one of the interfaces listed in Table 14-1 .) So you’d need to use CreateQuery<IDataR ecord> to create such a query, and a suitably modified loop to extract the results: var query = dbContext.CreateQuery<IDataRecord>(
Image of page 594

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 595
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern