<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Diego Vega : Entity SQL</title><link>http://blogs.msdn.com/diego/archive/tags/Entity+SQL/default.aspx</link><description>Tags: Entity SQL</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Some differences between ESQL and LINQ to Entities capabilities</title><link>http://blogs.msdn.com/diego/archive/2007/12/20/some-differences-between-esql-and-linq-to-entities-capabilities.aspx</link><pubDate>Thu, 20 Dec 2007 12:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6815101</guid><dc:creator>divega</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/diego/comments/6815101.aspx</comments><wfw:commentRss>http://blogs.msdn.com/diego/commentrss.aspx?PostID=6815101</wfw:commentRss><wfw:comment>http://blogs.msdn.com/diego/rsscomments.aspx?PostID=6815101</wfw:comment><description>&lt;P&gt;&lt;A href="http://codebetter.com/blogs/john.papa/" target=_blank mce_href="http://codebetter.com/blogs/john.papa/"&gt;John Papa&lt;/A&gt; asks in my comments about some differences among the two query languages.&lt;/P&gt;
&lt;P&gt;Let's start from the beginning:&lt;/P&gt;
&lt;H4&gt;What is almost the same&lt;/H4&gt;
&lt;P&gt;&lt;STRONG&gt;Updatable queries:&lt;/STRONG&gt; Neither LINQ to Entities nor ESQL currently enclose a real Data Manipulation Language. However, both can return tracked entities that you can then update and send back to the store. To make it clear, that is the case with ESQL only when you run your queries against Object Services (ObjectContext + ObjectQuery&amp;lt;T&amp;gt;), and not when you use it directly on top of Entity Services (EntityClient).&lt;/P&gt;
&lt;H4&gt;What is somewhat different&lt;/H4&gt;
&lt;P&gt;&lt;STRONG&gt;Dynamic queries:&lt;/STRONG&gt; Being a text based language, ESQL can be really very dynamic. You can get whatever you want by manipulating strings (which in my opinion can become dirty business :)). However, you better know what type will be returned from the query in order to use the result appropriately. I have seen some blog posts describing very smart ways to build dynamic LINQ queries without using any text based query languages. Those functional programming inspired techniques should be applicable in general to LINQ to Entities.&lt;/P&gt;
&lt;H4&gt;What is way different&lt;/H4&gt;
&lt;P&gt;&lt;STRONG&gt;Access to functions:&lt;/STRONG&gt; While in ESQL you have full access to EDM and store functions, in our LINQ implementation your options are currently limited to some CLR methods that we have mapped. For this we choose only methods that we can guarantee will behave consistently in a database store. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Association navigation:&lt;/STRONG&gt; In ESQL you can always navigate an association using NAVIGATE, even if there is no navigation property in the conceptual model. In LINQ if you don’t have a navigation property, you need to fall back to a JOIN.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Equal comparability:&lt;/STRONG&gt; In ESQL entities are “equal compare”, but not in LINQ to Entities. This affects the way you writ GROUP BY and JOIN operations in LINQ (not that you need them a lot if you have navigation properties).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Syntax:&lt;/STRONG&gt; In LINQ you basically get the nice query comprehension syntax, including very nice things like FROM first, LET, etc. In ESQL you get the nice SQL-like syntax, including SELECT first, and what else? ;)&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Result types: &lt;/STRONG&gt;You can get entity types, complex types, scalars, or IEnumerables of entity types from either ESQL or LINQ to Entities. Only LINQ returns anonymous types and IGroupings, and only ESQL queries can return DbDataRecords.&lt;/P&gt;
&lt;H4&gt;Conclusion&lt;/H4&gt;This is not a comprehensible list, just what I can tell from the top of my mind today. In general, very few of these differences are related to hard constraints. We actually have some designs to overcome many of the limitations of our LINQ implementation, and even some things we are thinking to borrow from LINQ to improve ESQL. 
&lt;P&gt;But it is very difficult to tell at this stage if any of those will make it for v1. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6815101" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/diego/archive/tags/LINQ+to+Entities/default.aspx">LINQ to Entities</category><category domain="http://blogs.msdn.com/diego/archive/tags/Entity+SQL/default.aspx">Entity SQL</category></item><item><title>Entity SQL Non-Quirkiness</title><link>http://blogs.msdn.com/diego/archive/2007/11/27/entity-sql-non-quirkiness.aspx</link><pubDate>Tue, 27 Nov 2007 14:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6548431</guid><dc:creator>divega</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/diego/comments/6548431.aspx</comments><wfw:commentRss>http://blogs.msdn.com/diego/commentrss.aspx?PostID=6548431</wfw:commentRss><wfw:comment>http://blogs.msdn.com/diego/rsscomments.aspx?PostID=6548431</wfw:comment><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/esql" target="_blank"&gt;Zlatko&lt;/a&gt; has been posting about one &lt;a href="http://blogs.msdn.com/esql/archive/2007/11/19/Compiled_5F00_Queries.aspx" target="_blank"&gt;LINQ to Entities new feature&lt;/a&gt; included in the upcoming Beta 3, so I decided to take revenge and make a 100% Entity SQL post. Here is something I ran against the other day:&lt;/p&gt;  &lt;p&gt;Let's assume we need to retrieve the Order with the maximum OrderID, which is a really awful way to get the ID of the order you just inserted! :) &lt;/p&gt;  &lt;p&gt;In your everyday store-specific SQL, you can use a MAX() aggregate function in a subquery as a WHERE predicate. In Transact SQL, it should look like this:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;font face="Courier New"&gt;SELECT&lt;/font&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="mso-tab-count: 1"&gt;&lt;font face="Courier New"&gt;&amp;#160;&lt;/font&gt;&lt;/span&gt;&lt;span style="color: gray"&gt;&lt;font face="Courier New"&gt;*            &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;font face="Courier New"&gt;FROM&lt;/font&gt;&lt;/span&gt;&lt;font face="Courier New"&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160; &lt;/span&gt;Products &lt;span style="color: blue"&gt;AS&lt;/span&gt; p           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;WHERE&lt;/span&gt;&lt;/font&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;font face="Courier New"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160; &lt;/span&gt;p&lt;span style="color: gray"&gt;.&lt;/span&gt;ProductID &lt;span style="color: gray"&gt;=(&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;p2&lt;span style="color: gray"&gt;.&lt;/span&gt;ProductID&lt;/font&gt;&lt;span style="color: gray"&gt;&lt;font face="Courier New"&gt;)&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;FROM&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160; &lt;/span&gt;Products &lt;span style="color: blue"&gt;as&lt;/span&gt; p2&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/font&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;So far, so good. If you have been playing a little with Entity SQL, you will probably guess how the equivalent Entity SQL would look like:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;span style="font-size: 10pt; color: blue; font-family: &amp;#39;Courier New&amp;#39;; mso-no-proof: yes"&gt;SELECT VALUE &lt;/span&gt;&lt;span style="font-size: 10pt; font-family: &amp;#39;Courier New&amp;#39;; mso-no-proof: yes"&gt;p        &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: &amp;#39;Courier New&amp;#39;; mso-no-proof: yes"&gt;FROM&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: &amp;#39;Courier New&amp;#39;; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160; &lt;/span&gt;Products &lt;span style="color: blue"&gt;AS&lt;/span&gt; p         &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: &amp;#39;Courier New&amp;#39;; mso-no-proof: yes"&gt;WHERE&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: &amp;#39;Courier New&amp;#39;; mso-no-proof: yes"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160; &lt;/span&gt;p&lt;span style="color: gray"&gt;.&lt;/span&gt;ProductID &lt;span style="color: gray"&gt;=(&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;&lt;span style="color: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;p2&lt;span style="color: gray"&gt;.&lt;/span&gt;ProductID&lt;span style="color: gray"&gt;)&amp;#160; &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: &amp;#39;Courier New&amp;#39;; mso-no-proof: yes"&gt;&lt;span style="color: blue"&gt;FROM&lt;/span&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160; &lt;/span&gt;Products &lt;span style="color: blue"&gt;as&lt;/span&gt; p2&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;But if you run this query, what you get is this interesting exception:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;font face="Courier New" color="#800000"&gt;System.Data.QueryException: Argument types 'Edm.Int32' and 'Transient.collection[Transient.rowtype[(_##groupAggMax2,Edm.Int32(Nullable=True,DefaultValue=))](Nullable=True,DefaultValue=)]' are incompatible for this operation, near WHERE predicate, line 1, column 60.&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;The subquery is actually returning a Transient.collection of a Transient.rowtype... Those are internal things, so for illustration purposes, let's turn to the LINQ perspective of life:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;var&lt;/span&gt; query = &lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; context.Products           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;font face="Courier New"&gt;&lt;span style="color: blue"&gt;select&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; { p.ProductID };           &lt;br /&gt;&lt;/font&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="color: blue"&gt;         &lt;br /&gt;&lt;font face="Courier New"&gt;int&lt;/font&gt;&lt;/span&gt;&lt;font face="Courier New"&gt; productID = query;&lt;/font&gt;&lt;/span&gt; &lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt;    &lt;p&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;(Argh, this post is no longer 100% Entity SQL!)&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;No surprisingly, what you get is a compile-time exception:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;font face="Courier New" color="#800000"&gt;Cannot implicitly convert type 'System.Linq.IQueryable&amp;lt;AnonymousType#1&amp;gt;' to 'int'.&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;Both exceptions are homologous, and for a text-based query language, Entity SQL happens to be very type-safe at its core. Standard SQL makes the basic assumption that it is ok to implicitly convert single-item collections of single-column projections to discrete scalars. We don't. &lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;The basic theme in Version 1.0 of the Entity Framework is to build a solid foundation for the future. As a consequence, one thing we avoid doing is &amp;quot;magic&amp;quot; type conversions except when they make perfect sense (think union of projection queries with exactly the same shape). The motive: magic conversions tend to mine the stability and composability of the language. &lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;That said, this buys us freedom to hand-pick certain implicit behavior in the future, if we find enough feedback and proof that it makes sense.&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;That's enough on the rationale. Now, how do I make it work? There are two approaches. &lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;First:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;font face="Courier New"&gt;SELECT &lt;/font&gt;&lt;/span&gt;&lt;font face="Courier New"&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="color: blue"&gt;VALUE&lt;/span&gt; p           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;FROM&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160; &lt;/span&gt;Products &lt;span style="color: blue"&gt;AS&lt;/span&gt; p           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;WHERE&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160; &lt;/span&gt;p&lt;span style="color: gray"&gt;.&lt;/span&gt;ProductID &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT&lt;/span&gt; &lt;span style="color: blue"&gt;VALUE&lt;/span&gt; p2&lt;span style="color: gray"&gt;.&lt;/span&gt;ProductID           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="color: blue"&gt;FROM&lt;/span&gt;&amp;#160;&amp;#160; Products &lt;span style="color: blue"&gt;AS&lt;/span&gt; p2&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;This one works because:&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;a) The SELECT VALUE returns the scalar itself, instead of a projection (rowtype) of the scalar.&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;b) MAX() operates on the collection of scalars returned by the subquery, returning a single maximum value that will be directly comparable (same type) as ProductID.&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;Second:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;font face="Courier New"&gt;SELECT&lt;/font&gt;&lt;/span&gt;&lt;font face="Courier New"&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="mso-tab-count: 1"&gt; &lt;/span&gt;&lt;span style="color: blue"&gt;VALUE&lt;/span&gt; p           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;FROM&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160;&amp;#160; &lt;/span&gt;Products &lt;span style="color: blue"&gt;AS&lt;/span&gt; p           &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; color: blue; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;WHERE&lt;/span&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;span style="font-size: 10pt; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="mso-tab-count: 1"&gt;&amp;#160; &lt;/span&gt;p&lt;span style="color: gray"&gt;.&lt;/span&gt;ProductID &lt;span style="color: gray"&gt;=&lt;/span&gt; ANYELEMENT&lt;span style="color: gray"&gt;(           &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/span&gt;&lt;span style="color: blue"&gt;SELECT VALUE&lt;/span&gt; &lt;span style="color: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;p2&lt;span style="color: gray"&gt;.&lt;/span&gt;ProductID&lt;span style="color: gray"&gt;)&lt;/span&gt;&amp;#160;&amp;#160; &lt;br /&gt;&lt;/span&gt;&lt;span style="font-size: 10pt; line-height: 115%; font-family: " yes?="yes?" mso-no-proof:="mso-no-proof:" new?;="new?;" courier="courier"&gt;&lt;span style="color: blue"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; FROM&lt;/span&gt;&amp;#160;&amp;#160; Products &lt;span style="color: blue"&gt;AS&lt;/span&gt; p2&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/font&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;This works because:&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;a) The subquery will return a single-item collection of a scalar value.&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;b) ANYELEMENT will retrieve a single element (in this case, the only one) contained in the collection. That element will be directly comparable with ProductID.&lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;In case you are wondering now how efficient this is, don't worry. Entity SQL is still a functional language. So, while understanding the type reasoning is interesting and useful, these queries still express &amp;quot;what you want to get&amp;quot; rather than &amp;quot;how you want the job done&amp;quot;. &lt;/p&gt;  &lt;p class="MsoNormal" style="margin-bottom: 0pt; line-height: normal; mso-layout-grid-align: none"&gt;As a matter of fact, with our current SqlClient implementation, these queries will be translated to some simple, yet unexpected Transact-SQL. But I'll leave that to you as an exercise...&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6548431" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/diego/archive/tags/Entity+SQL/default.aspx">Entity SQL</category></item><item><title>Choosing an Entity Framework API</title><link>http://blogs.msdn.com/diego/archive/2007/11/11/choosing-an-entity-framework-api.aspx</link><pubDate>Sun, 11 Nov 2007 15:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6099109</guid><dc:creator>divega</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/diego/comments/6099109.aspx</comments><wfw:commentRss>http://blogs.msdn.com/diego/commentrss.aspx?PostID=6099109</wfw:commentRss><wfw:comment>http://blogs.msdn.com/diego/rsscomments.aspx?PostID=6099109</wfw:comment><description>&lt;P&gt;Last month, a question was asked in the &lt;A href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2252416&amp;amp;SiteID=1" target=_blank mce_href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2252416&amp;amp;SiteID=1"&gt;ADO.NET Prerelease&lt;/A&gt; forum that went more or less like this:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;I&gt;Considering that there are many APIs you can use (Entity SQL, ObjectQuery&amp;lt;T&amp;gt;, LINQ to Entities), is there any guidance that could help me decide when to use each?&lt;/I&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The best I could do based on my knowledge at the time: &lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;I&gt;It is matter of taste.&lt;/I&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;While my answer was partially correct and had the great quality of being easy to look at, I immediately realized I should do a better job in helping people choose the appropriate API for each of their scenarios. &lt;/P&gt;
&lt;P&gt;I won’t pretend here to give the definitive and detailed answer, just a head start. You will find more information in our docs and I am sure this topic alone will easily fill a few chapters in upcoming books about the product.&lt;/P&gt;
&lt;H4&gt;Service Layers and Query languages&lt;/H4&gt;
&lt;P&gt;We basically support two distinct programming layers and two different query languages your applications can use: &lt;/P&gt;
&lt;TABLE class=MsoTableMediumGrid3Accent5 style="BORDER-TOP-STYLE: none; BORDER-RIGHT-STYLE: none; BORDER-LEFT-STYLE: none; BORDER-COLLAPSE: collapse; BORDER-BOTTOM-STYLE: none" cellSpacing=0 cellPadding=0 border=1 class="MsoTableMediumGrid3Accent5"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: white 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #4bacc6; PADDING-BOTTOM: 0in; BORDER-LEFT: white 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: 0px" vAlign=top width=319 colSpan=2 rowSpan=rowspan&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;&lt;B&gt;&lt;SPAN style="COLOR: white"&gt;Service layers and query languages supported&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: white 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #4bacc6; PADDING-BOTTOM: 0in; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: white 3pt solid; BORDER-LEFT-STYLE: none" vAlign=top width=319 colSpan=2&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;&lt;B&gt;&lt;SPAN style="COLOR: white"&gt;Query language&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: 0px solid; PADDING-LEFT: 5.4pt; BACKGROUND: #4bacc6; PADDING-BOTTOM: 0in; BORDER-LEFT: white 1pt solid; WIDTH: 239.4pt; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt" vAlign=top width=319 colSpan=2 rowSpan=rowspan&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;&lt;B&gt;&lt;SPAN style="COLOR: white"&gt;&lt;/SPAN&gt;&lt;/B&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: #a5d5e2; PADDING-BOTTOM: 0in; WIDTH: 119.7pt; BORDER-TOP-STYLE: none; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid; BORDER-LEFT-STYLE: none" vAlign=top width=160&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;Entity SQL&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: #a5d5e2; PADDING-BOTTOM: 0in; WIDTH: 119.7pt; BORDER-TOP-STYLE: none; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid; BORDER-LEFT-STYLE: none" vAlign=top width=160&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;LINQ Queries&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: white 3pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: white 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #4bacc6; PADDING-BOTTOM: 0in; BORDER-LEFT: white 1pt solid; WIDTH: 119.7pt; PADDING-TOP: 0in; BORDER-BOTTOM: 0px solid" vAlign=top width=160 rowSpan=rowspan&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;&lt;B&gt;&lt;SPAN style="COLOR: white"&gt;Service layer&lt;/SPAN&gt;&lt;/B&gt;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: white 1pt solid; PADDING-LEFT: 5.4pt; BACKGROUND: #d2eaf1; PADDING-BOTTOM: 0in; BORDER-LEFT: white 1pt; WIDTH: 119.7pt; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid" vAlign=top width=160&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;Entity Services&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: #d2eaf1; PADDING-BOTTOM: 0in; WIDTH: 119.7pt; BORDER-TOP-STYLE: none; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid; BORDER-LEFT-STYLE: none" vAlign=top width=160&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;Yes&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: #d2eaf1; PADDING-BOTTOM: 0in; WIDTH: 119.7pt; BORDER-TOP-STYLE: none; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid; BORDER-LEFT-STYLE: none" vAlign=top width=160&gt;&lt;/TD&gt;&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="" style="BORDER-RIGHT: white 3pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: 0px solid; PADDING-LEFT: 5.4pt; BACKGROUND: #4bacc6; PADDING-BOTTOM: 0in; BORDER-LEFT: white 1pt solid; WIDTH: 119.7pt; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid" vAlign=top width=160 rowSpan=rowspan&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;&lt;B&gt;&lt;SPAN style="COLOR: white"&gt;&lt;/SPAN&gt;&lt;/B&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: #a5d5e2; PADDING-BOTTOM: 0in; WIDTH: 119.7pt; BORDER-TOP-STYLE: none; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid; BORDER-LEFT-STYLE: none" vAlign=top width=160&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;Object Services&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: #a5d5e2; PADDING-BOTTOM: 0in; WIDTH: 119.7pt; BORDER-TOP-STYLE: none; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid; BORDER-LEFT-STYLE: none" vAlign=top width=160&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;Yes&lt;/P&gt;&lt;/TD&gt;
&lt;TD class="" style="BORDER-RIGHT: white 1pt solid; PADDING-RIGHT: 5.4pt; PADDING-LEFT: 5.4pt; BACKGROUND: #a5d5e2; PADDING-BOTTOM: 0in; WIDTH: 119.7pt; BORDER-TOP-STYLE: none; PADDING-TOP: 0in; BORDER-BOTTOM: white 1pt solid; BORDER-LEFT-STYLE: none" vAlign=top width=160&gt;
&lt;P class=MsoNormal style="MARGIN-BOTTOM: 0pt; LINE-HEIGHT: normal"&gt;Yes&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;
&lt;P mce_keep="true"&gt;For those coming from the Object/Relational Mapping world, one easy way to look at our stack is to understand that we have two mapping tools layered one on top of the other: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;An Entity/Relational Mapper known as Entity Services. &lt;/LI&gt;
&lt;LI&gt;An Object/Entity Mapper named Object Services. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Of course, once you have mapped your relational tables to entities and your entities to objects, what you get is a fully functional O/R Mapper.&lt;/P&gt;
&lt;P&gt;But as it&amp;nbsp;is usual in our profession, adding a level of indirection uncovers a lot of power and flexibility :)&lt;/P&gt;
&lt;H4&gt;First Service Layer: Entity Services&lt;/H4&gt;
&lt;P&gt;The public surface of this layer is the EntityClient component, which is a new type of ADO.NET provider that gives you access to a store agnostic entity-relationship model of your data called Entity Data Model (EDM), and decouples your code from the store specific relational model that lives underneath. &lt;/P&gt;
&lt;P&gt;Besides a pair of new classes, the EntityClient contains most of the same types as previous providers: Connection, Command, DataReader, Parameter, Adapter, Transactions and a ProviderFactory. &lt;/P&gt;
&lt;P&gt;To be able to use this layer, you typically need three elements: &lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;STRONG&gt;ADO.NET provider that is specific to your database engine and has been extended to work with the Entity Framework.&lt;/STRONG&gt; &lt;I&gt;Basically, the extensions involve the inclusion of a detailed provider manifest,support for command objects consisting of command trees and the ability to generate store specific SQL from those command trees. &lt;/I&gt;&lt;I&gt;An appropriate provider for SQL Server will be included with the Entity Framework, and various provider writers are working right now to give you access to non-Microsoft relational databases.&lt;/I&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Mapping information in the form of SSDL, CSDL, and MSL files that describe your storage model, your application’s conceptual model and the mapping among the two. &lt;/STRONG&gt;&lt;I&gt;More recently we have added EDMX, a format that packages all the mapping information in a single file at design-time.&lt;/I&gt; &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Queries expressed in Entity SQL (eSQL), which is a new dialect of SQL that delivers the power of the Entity Framework. &lt;/STRONG&gt;&lt;I&gt;Typically, the EntityClient will take a string containing eSQL everywhere your regular provider would accept a string containing store specific SQL. &lt;/I&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;One advantage of programming against this layer is that being the first public surface intended for application development, it is also the most lightweight. &lt;/P&gt;
&lt;P&gt;Moreover, at this level you use full eSQL queries to obtain data readers and not actual entity classes. For this reason, we call EntityClient our &lt;B&gt;“value” oriented&lt;/B&gt; programming interface. Neither the columns included in your rows, nor the source of your rows, nor the filtering, grouping or sorting criteria, are fixed at compile time. The query is just a string that we parse at run-time, and the results are just EntityDataReaders.&lt;/P&gt;
&lt;P&gt;All this makes Entity Services suitable for applications that today typically exploit the flexibility of writing dynamic SQL queries, like reporting, ad-hoc querying, etc. &lt;/P&gt;
&lt;P&gt;Notice however, that even when the EntityClient closely follows the traditional ADO.NET connected object model, you cannot get an ADO.NET DataSet on top. There are two main reasons for this:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The DataSet does not have the necessary constructs to represent the variety of relationships the EDM can support.&lt;/LI&gt;
&lt;LI&gt;The EntityClient does not support the metadata protocols used to create the DataSet schema.&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Moreover, the Entity Framework currently lacks a string based data manipulation language, so you cannot directly express UPDATE, INSERT and DELETE operations in eSQL. Given this, our EntityAdapter is hardly any similar to the previous DataAdapters. We do not even derive it from the DbDataAdapter class! &lt;/P&gt;
&lt;H4&gt;Second Service Layer: Object Services&lt;/H4&gt;
&lt;P&gt;Object Services lives immediately on top of the EntityClient, and provides your application an Object Oriented view your data. Many public classes live in this space, but the two most important are ObjectContext and ObjectQuery&amp;lt;T&amp;gt;.&lt;/P&gt;
&lt;H5&gt;ObjectContext&lt;/H5&gt;
&lt;P&gt;This object’s main role is to encapsulate the underlying EntityConnection, and serve as a porthole for objects performing CRUD operations. &lt;/P&gt;
&lt;P&gt;When you choose to use our code generation, you get a type-safe ObjectContext that incorporates some methods specific to your data model. &lt;/P&gt;
&lt;H5&gt;ObjectQuery&amp;lt;T&amp;gt;&lt;/H5&gt;
&lt;P&gt;ObjectQuery&amp;lt;T&amp;gt; and its builder methods let you create queries in an completely object oriented way. It also provides a type-safe way to create queries. Most of the time, the shape and source of your data, the filtering, grouping and sorting criteria are known at compile time. So we call this our &lt;B&gt;object-oriented&lt;/B&gt; programming interface.&lt;/P&gt;
&lt;P&gt;You can still use fragments of eSQL with many builder methods, but the idea here is that you typically use ObjectQuery&amp;lt;T&amp;gt; in an early-bound manner to build queries that get compiled in your application. Even more important, the results of those queries can be full entity classes or new types created for projections.&lt;/P&gt;
&lt;H4&gt;First Query Language: Entity SQL &lt;/H4&gt;
&lt;P&gt;Entity-SQL is a text based query language that currently gives you the most expressiveness over the Entity Framework stack on late-bound scenarios. You can use Entity-SQL to get collections of rows in the Entity Services layer, but also instances of entity classes, when used with Object Services. &lt;/P&gt;
&lt;P&gt;I highly recommend reading Zlatko Michailov’s &lt;A href="http://blogs.msdn.com/zlatkom/archive/2007/07/10/entity-sql.aspx" mce_href="http://blogs.msdn.com/zlatkom/archive/2007/07/10/entity-sql.aspx"&gt;Entity SQL post&lt;/A&gt; for a head start on the language and on its main differences with traditional SQL.&lt;/P&gt;
&lt;H4&gt;Second Query Language: LINQ Query Comprehensions&lt;/H4&gt;
&lt;P&gt;The Language Integrated Query is a set of strategic language extension Microsoft is including both in C# and VB that facilitate the creation of query expressions using a terse syntax familiar to anyone who has used SQL. &lt;/P&gt;
&lt;P&gt;LINQ is very powerful, and it is broadly applicable since it aims to solve the problem of querying any data source, including objects in memory, databases and XML files while maintaining a consistent, object-oriented and type-safe programming interface.&lt;/P&gt;
&lt;P&gt;For the Entity Framework, ObjectQuery&amp;lt;T&amp;gt; is the center of our LINQ implementation. This class implements the necessary interfaces to fully support&amp;nbsp;the creation and deferred execution of queries comprehensions against our stack. &lt;/P&gt;
&lt;P&gt;We have invested a great amount of work in&amp;nbsp;correctly mapping CLR features that can be useful in queries to our EDM and query capabilities. Still, LINQ and the Entity Framework are built and optimized against different goals and assumptions, and some concepts of LINQ and the Entity Framework simply do not map one-to-one. &lt;/P&gt;
&lt;P&gt;We certainly plan to continue investing in better alignment. But right now the reality&amp;nbsp;is that&amp;nbsp;there are some things you can do with Entity SQL that still cannot be expressed in LINQ, and there are a few things you can do with LINQ that still we cannot be translated or compose over in our LINQ implementation.&lt;/P&gt;
&lt;H4&gt;Conclusion&lt;/H4&gt;
&lt;P&gt;My original answer stays correct: Using one or other API to create your applications also has to do with a matter of taste. This is specially true thanks to the flexibility of ObjectQuery&amp;lt;T&amp;gt;, which allows you to &lt;STRIKE&gt;mix and match&lt;/STRIKE&gt; start with query building methods that take eSQL fragments &lt;STRIKE&gt;inside&lt;/STRIKE&gt; or LINQ queries. Just be aware that you could run into some corners scenarios in which we cannot completely go from one model to the other and back.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Edit: The assertion that you can mix and match LINQ and ESQL was incorrect. Once you started one way, you have to keep in that route in ObjectQuery&amp;lt;T&amp;gt;.&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6099109" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/diego/archive/tags/LINQ+to+Entities/default.aspx">LINQ to Entities</category><category domain="http://blogs.msdn.com/diego/archive/tags/Entity+SQL/default.aspx">Entity SQL</category></item></channel></rss>