<?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>With Great Power comes Great Response.write("Ability") : Linq</title><link>http://blogs.msdn.com/phaniraj/archive/tags/Linq/default.aspx</link><description>Tags: Linq</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Set Based Operations in Ado.net Data Services Part II</title><link>http://blogs.msdn.com/phaniraj/archive/2009/05/27/set-based-operations-in-ado-net-data-services-part-ii.aspx</link><pubDate>Wed, 27 May 2009 08:59:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9643661</guid><dc:creator>PhaniRajuYN</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/phaniraj/comments/9643661.aspx</comments><wfw:commentRss>http://blogs.msdn.com/phaniraj/commentrss.aspx?PostID=9643661</wfw:commentRss><description>&lt;P&gt;As an extension to the last blog post dealing with &lt;A href="http://blogs.msdn.com/phaniraj/archive/2008/07/17/set-based-operations-in-ado-net-data-services.aspx" mce_href="http://blogs.msdn.com/phaniraj/archive/2008/07/17/set-based-operations-in-ado-net-data-services.aspx"&gt;Set based filter operations in our client library&lt;/A&gt; ,&lt;BR&gt;we will introduce support for the specifying method calls in the filter expression.&lt;/P&gt;&lt;B&gt;What does this achieve ?&lt;/B&gt;&lt;BR&gt;Currently , the IsIn operator only supports an equality comparision. &lt;BR&gt;With support for Method Calls , you can now select entities in a set which when passed to a method , evaluate true. ex: You can generate Uris such as this : &lt;BR&gt;
&lt;OL&gt;
&lt;LI&gt;/northwind.svc/Customers?$filter = substringof('London',City) or substringof('Berlin',City) or substringof('Prague',City) 
&lt;LI&gt;/northwind.svc/Customers?$filter = startswith('London',City) or startswith('Berlin',City) or startswith('Prague',City) 
&lt;LI&gt;/northwind.svc/Customers?$filter = endswith('London',City) or endswith('Berlin',City) or endswith('Prague',City) &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Fortunately , not a lot of code change is required to get this support.&lt;/P&gt;
&lt;P&gt;We will change the first parameter of the extension method from &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TEntity, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;&amp;gt; propertyExpression&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;to &lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TEntity, TValue, &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt;&amp;gt;&amp;gt; comparisionInvokeExpression&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;which means that where we were initially sending an expression that selects a property of the entity , we now send the Extension method a delegate that accepts the entity and the value being compared against it and returns a boolean value after comparison using a method.&lt;/P&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;example : &lt;BR&gt;&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;T, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;&amp;gt; propertyExpression &lt;STRONG&gt;&lt;EM&gt;means&lt;/EM&gt;&lt;/STRONG&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;customer =&amp;gt; customer.City&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;TEntity, TValue, &lt;SPAN style="COLOR: blue"&gt;bool&lt;/SPAN&gt;&amp;gt;&amp;gt; comparisionInvokeExpression &lt;STRONG&gt;&lt;EM&gt;means&lt;/EM&gt;&lt;/STRONG&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;(cust, cityName) =&amp;gt; cust.City.ToLower().StartsWith(cityName)&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The second change is in the location where we build the comparision expression for values in the set. &lt;BR&gt;we change the line which does the comparision using Expression.Equal with a method call to the comparision expression passed in . &lt;BR&gt;We will change&amp;nbsp; :&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: green"&gt;//Build a comparision expression which equats the Id of the ENtity with this value in the IDs list
// ex : e.Id == 1
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression &lt;/SPAN&gt;comparison = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Equal(left, &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Constant(id));&lt;/PRE&gt;to : &lt;BR&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: green"&gt;//The Left Hand Side of the Filter Expression
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MethodCallExpression &lt;/SPAN&gt;comaprisionMethod = comparisionInvokeExpression.Body &lt;SPAN style="COLOR: blue"&gt;as &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;MethodCallExpression&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: green"&gt;//Build a comparision expression which calls the method that does the comparision for us
//ex : c=&amp;gt; c.City.Contains(id)
&lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;Expression &lt;/SPAN&gt;comparison = &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Call(
                        comaprisionMethod.Object,
                        comaprisionMethod.Method,
                        &lt;SPAN style="COLOR: #2b91af"&gt;Expression&lt;/SPAN&gt;.Constant(id) );&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;The complete code sample is here : &lt;BR&gt;
&lt;SCRIPT src="http://gist.github.com/117227.js" mce_src="http://gist.github.com/117227.js"&gt;&lt;/SCRIPT&gt;
&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9643661" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Astoria/default.aspx">Astoria</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Demo/default.aspx">Demo</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/DataServiceContext/default.aspx">DataServiceContext</category><category domain="http://blogs.msdn.com/phaniraj/archive/tags/Linq/default.aspx">Linq</category></item></channel></rss>