<?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>Entity Framework Design : eSQL</title><link>http://blogs.msdn.com/efdesign/archive/tags/eSQL/default.aspx</link><description>Tags: eSQL</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Model Defined Functions</title><link>http://blogs.msdn.com/efdesign/archive/2009/01/07/model-defined-functions.aspx</link><pubDate>Wed, 07 Jan 2009 23:32:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9290047</guid><dc:creator>efdesign</dc:creator><slash:comments>21</slash:comments><comments>http://blogs.msdn.com/efdesign/comments/9290047.aspx</comments><wfw:commentRss>http://blogs.msdn.com/efdesign/commentrss.aspx?PostID=9290047</wfw:commentRss><description>&lt;P&gt;Today the Entity Framework, and more specifically the Entity Data Model, have a limited notion of Functions.&lt;/P&gt;
&lt;P&gt;We are currently restricted to Function Imports that allow stored procedures to be invoked, and Canonical / Store Functions for database independent and database specific functions respectively.&lt;/P&gt;
&lt;P&gt;Now however we want to &lt;SPAN style="mso-fareast-language: ja"&gt;support functions defined, not just declared, in the EDM (&lt;A href="http://blogs.msdn.com/alexj/archive/2008/11/24/edm-misconceptions.aspx" mce_href="http://blogs.msdn.com/alexj/archive/2008/11/24/edm-misconceptions.aspx"&gt;aka. the CSDL&lt;/A&gt;). &lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="mso-fareast-language: ja"&gt;An example would be: &lt;/SPAN&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN-BOTTOM: 0pt; mso-layout-grid-align: none; mso-outline-level: 1" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: #a31515; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;Function&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: red; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;=&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;"&lt;SPAN style="COLOR: blue"&gt;GetAge&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;ReturnType&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Edm.Int32&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: #a31515; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;Parameter&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: red; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;=&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;"&lt;SPAN style="COLOR: blue"&gt;Person&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Model.Person&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; /&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: #a31515; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;DefiningExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&amp;gt;&amp;nbsp;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="mso-tab-count: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;FONT color=#000000&gt;Edm.DiffYears(Edm.CurrentDateTime(), Person.Birthday)&lt;/FONT&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: #a31515; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;DefiningExpression&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: ; COLOR: #a31515; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;Function&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&amp;gt; &lt;/SPAN&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="mso-no-proof: yes"&gt;Here are some things to notice:&lt;/SPAN&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;The DefiningExpression is eSQL.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;The function can have zero or more parameters.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;The Function must have a return type.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;Function Parameters are referenced directly by Name in the DefiningExpression: meaning there is no parameter denoting prefix like @. This means you must be careful to choose parameter names that don't collide with identifiers you need to use in the rest of eSQL expression.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes"&gt;Unlike functions in SSDL, functions in CSDL only support In bound Parameters (i.e &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Mode&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;=&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;"&lt;SPAN style="COLOR: blue"&gt;In&lt;/SPAN&gt;") &lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: calibri; mso-hansi-font-family: calibri; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;because otherwise they become non-composable.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: calibri; mso-hansi-font-family: calibri; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: calibri; mso-hansi-font-family: calibri; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;For this reason the &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Mode&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: calibri; mso-hansi-font-family: calibri; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;of a parameter cannot be set in CSDL (it is always &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Mode&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;=&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;"&lt;SPAN style="COLOR: blue"&gt;In&lt;/SPAN&gt;"&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes; mso-bidi-font-family: 'Courier New'; mso-ascii-font-family: calibri; mso-hansi-font-family: calibri; mso-ascii-theme-font: minor-latin; mso-hansi-theme-font: minor-latin"&gt;).&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;SPAN style="mso-tab-count: 1"&gt; &lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;Functions are declared as Global Items and are declared&amp;nbsp;within the &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Schema&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;gt; &lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes"&gt;element. As such there identity is made up of the Schema's namespace and the function name.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;The function parameters and return type can be any of the following:&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;UL&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;A scalar type or collection of scalar types.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes"&gt;An entity type or collection of entity types.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes"&gt;A complex type or collection of complex types.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes"&gt;A row type or collection of row types (See below).&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes"&gt;A ref type or collection of ref types.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;/SPAN&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;SPAN style="mso-no-proof: yes"&gt;Functions with a DefiningExpression do not require mapping, since the eSQL expression is composed out of eSQL fragments that are already mapped.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN style="mso-no-proof: yes"&gt;&lt;SPAN style="mso-no-proof: yes"&gt;Functions without a DefiningExpression are simply declarations. Today the Entity Framework doesn't complain when loading a CSDL with such a function, but you can't invoke it. In the future these functions might be used to support mapping Table Value Functions in the store to functions in the Conceptual Model.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;Since it is trivial in eSQL to create arbitrary un-named types, imagine a projection that projects 3 of the properties from an Entity, we now need a mechanism for defining these "RowTypes" inline, so that they can be used when defining Function Parameters and ReturnTypes. For example:&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes; mso-fareast-language: ja"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes; mso-fareast-language: ja"&gt;Parameter&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;=&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;"&lt;SPAN style="COLOR: blue"&gt;Coordinate&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;RowType&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Property&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;=&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;"&lt;SPAN style="COLOR: blue"&gt;X&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Nullable&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Property&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Y&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Nullable&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Property&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Z&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Nullable&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;RowType&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Parameter&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;Since eSQL is primarily set based, we also need a way of defining parameters and return types that are collections of RowTypes: &lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes; mso-fareast-language: ja"&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes; mso-fareast-language: ja"&gt;Parameter&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;=&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;"&lt;SPAN style="COLOR: blue"&gt;Coordinates&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;&amp;gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;CollectionType&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;RowType&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Property&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: red; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;=&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;"&lt;SPAN style="COLOR: blue"&gt;X&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Nullable&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Property&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Y&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Nullable&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt; &lt;BR&gt;&lt;SPAN style="mso-tab-count: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&amp;lt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;Property&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Name&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;Z&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Type&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: red"&gt;Nullable&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;=&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;false&lt;/SPAN&gt;"&lt;SPAN style="COLOR: blue"&gt;/&amp;gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="COLOR: #a31515"&gt;RowType&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;gt; &lt;BR&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;CollectionType&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;gt;&lt;/SPAN&gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;lt;/&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: #a31515; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;Parameter&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&amp;gt;&lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;H3&gt;Using the Function via eSQL:&lt;/H3&gt;
&lt;P&gt;It is trivial to use the function via eSQL. For example:&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: #17365d" new?;="new?;" courier="courier" mso-themecolor:="mso-themecolor:" mso-themeshade:="mso-themeshade:" 191?="191?" text2;="text2;" JA;="JA;" mso-fareast-language:="mso-fareast-language:"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: " new?;="new?;" courier="courier" mso-fareast-language:="mso-fareast-language:" JA?="JA?"&gt; Namespace.GetAge(p) &lt;BR&gt;&lt;SPAN style="COLOR: #17365d; mso-themecolor: text2; mso-themeshade: 191"&gt;FROM&lt;/SPAN&gt; Container.People &lt;SPAN style="COLOR: #17365d; mso-themecolor: text2; mso-themeshade: 191"&gt;AS&lt;/SPAN&gt; P &lt;BR&gt;&lt;SPAN style="COLOR: #17365d; mso-themecolor: text2; mso-themeshade: 191"&gt;WHERE&lt;/SPAN&gt; P.Firstname = &lt;SPAN style="COLOR: red"&gt;‘Jim’ &lt;/SPAN&gt;&lt;/SPAN&gt;
&lt;P&gt;Here we get Jim's age, assuming of course there is only one Jim!&lt;/P&gt;
&lt;P&gt;It is also possible to compose functions together, you must simply ensure return types and target parameter types are the same (in the case of named types) or structurally equivalent (in the case of row types or collections of row types).&lt;/P&gt;
&lt;P&gt;Things get a little trickier when you are dealing with functions that return sets, for example imagine a function that returns someone's friends, used in conjunction with the GetAge function:&lt;/P&gt;
&lt;P class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: #1f497d" new?;="new?;" courier="courier" mso-themecolor:="mso-themecolor:" text2?="text2?"&gt;SELECT&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: " courier="courier" new??="new??"&gt; &lt;SPAN style="COLOR: #1f497d; mso-themecolor: text2"&gt;VALUE&lt;/SPAN&gt; (F) &lt;BR&gt;&lt;SPAN style="COLOR: #1f497d; mso-themecolor: text2"&gt;FROM&lt;/SPAN&gt; Container.People &lt;SPAN style="COLOR: #1f497d; mso-themecolor: text2"&gt;AS&lt;/SPAN&gt; P &lt;BR&gt;&lt;SPAN style="COLOR: #1f497d; mso-themecolor: text2"&gt;CROSS&lt;/SPAN&gt; &lt;SPAN style="COLOR: #1f497d; mso-themecolor: text2"&gt;APPLY&lt;/SPAN&gt; Namespace.GetFriends(P) &lt;SPAN style="COLOR: #1f497d; mso-themecolor: text2"&gt;AS&lt;/SPAN&gt; F &lt;BR&gt;&lt;SPAN style="COLOR: #1f497d; mso-themecolor: text2"&gt;WHERE&lt;/SPAN&gt; Namespace.GetAge(P) &amp;gt; 21 &lt;/SPAN&gt;
&lt;P&gt;Here we get all the friends of people older than 21.&lt;/P&gt;
&lt;P&gt;As you can see to do this sort of thing you need a crash course in eSQL. &lt;/P&gt;
&lt;H3&gt;Using the Function via LINQ:&lt;/H3&gt;
&lt;P&gt;It is also possible to use these functions in LINQ, but this does require a extra step to create an appropriate stub function in the CLR.&lt;/P&gt;
&lt;P&gt;This solution is based on the techniques described &lt;A href="http://blogs.msdn.com/efdesign/archive/2008/10/08/edm-and-store-functions-exposed-in-linq.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/10/08/edm-and-store-functions-exposed-in-linq.aspx"&gt;here&lt;/A&gt;, and involves creating a Stub function in the CLR language of your choice, and annotating it something like this:&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN-BOTTOM: 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;[&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;EdmFunction&lt;/SPAN&gt;&lt;/SPAN&gt;(&lt;FONT color=#ff0000&gt;"Namespace"&lt;/FONT&gt;, &lt;FONT color=#ff0000&gt;"GetAge"&lt;/FONT&gt;)] &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;public&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt; &lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;static &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;int &lt;/SPAN&gt;GetAge(&lt;SPAN style="COLOR: #2b91af"&gt;Person&lt;/SPAN&gt; p) &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;{&amp;nbsp; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;throw new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #31849b; mso-themecolor: accent5; mso-themeshade: 191"&gt;NotSupportedException&lt;/SPAN&gt;&lt;SPAN style="COLOR: #1f497d; mso-themecolor: text2"&gt;(…); &lt;BR&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;}&lt;/SPAN&gt;&lt;SPAN style="mso-fareast-language: ja"&gt; &lt;/SPAN&gt;
&lt;P&gt;The Entity Framework uses the signature of the function and the EdmFunction attribute to map calls to this function when encountered to the appropriate Model Defined Function.&lt;/P&gt;
&lt;P&gt;Once you have this stub it is then trivial to use it in a LINQ query like this:&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-FAMILY: ; COLOR: blue; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;var&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt; peopleOver21 = &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&lt;/SPAN&gt; p &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ctx&lt;/SPAN&gt;.People &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where&lt;/SPAN&gt; GetAge(p) &amp;lt; 21 &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: ; FONT-SIZE: 10pt" new?;="new?;" courier="courier" yes?="yes?" mso-no-proof:="mso-no-proof:"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&lt;/SPAN&gt; p; &lt;/SPAN&gt;
&lt;P&gt;Indeed if you are familiar with LINQ you will probably find composing functions together a lot easier too:&lt;/P&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: blue; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;var&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; friendOfPeopleOver21 = &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; from&lt;/SPAN&gt; p &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ctx&lt;/SPAN&gt;.People &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt;&amp;nbsp;&lt;FONT color=#000000&gt;f&lt;/FONT&gt; &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt;&amp;nbsp;&lt;FONT color=#000000&gt;GetFriends(p)&lt;/FONT&gt; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where&lt;/SPAN&gt; GetAge(p) &amp;lt; 21 &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; select&lt;/SPAN&gt; f; 
&lt;P mce_keep="true"&gt;&lt;/SPAN&gt;Notice that the CLR functions don't need to be directly callable, in the example above the CLR stub throws an exception if called directly. &lt;/P&gt;
&lt;P&gt;However the existence of the stub allows you to create LINQ expressions that compile correctly, and then at runtime, when used in a LINQ to Entities query, the function call is simply translated by the entity framework into a query that runs in the database.&lt;/P&gt;
&lt;H3&gt;Summary&lt;/H3&gt;
&lt;P&gt;As you can see Model Defined Function's are very powerful, and this post has barely scratched the surface of possibilities they open up.&lt;/P&gt;
&lt;P&gt;The Entity Framework team would love to hear your comments.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;A href="http://blogs.msdn.com/alexj" mce_href="http://blogs.msdn.com/alexj"&gt;Alex James&lt;/A&gt;&lt;/STRONG&gt;&amp;nbsp; &lt;BR&gt;Program Manager, &lt;BR&gt;Entity Framework Team&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;EM&gt;This post is part of the transparent design exercise in the Entity Framework Team. To understand how it works and how your feedback will be used please look at &lt;A href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx" mce_href="http://blogs.msdn.com/efdesign/archive/2008/06/23/transparency-in-the-design-process.aspx"&gt;this post&lt;/A&gt;.&lt;/EM&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9290047" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework/default.aspx">Entity Framework</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/EDM/default.aspx">EDM</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Linq+to+Entities/default.aspx">Linq to Entities</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Functions/default.aspx">Functions</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/eSQL/default.aspx">eSQL</category><category domain="http://blogs.msdn.com/efdesign/archive/tags/Entity+Framework+4/default.aspx">Entity Framework 4</category></item></channel></rss>