<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>C# Disciples &#187; lambda</title>
	<atom:link href="http://marlongrech.wordpress.com/category/lambda/feed/" rel="self" type="application/rss+xml" />
	<link>http://marlongrech.wordpress.com</link>
	<description>my life in Avalon ....</description>
	<lastBuildDate>Thu, 15 Oct 2009 09:19:13 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='marlongrech.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/0c938eec94507e7222db1a9e8ad7ba9e?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>C# Disciples &#187; lambda</title>
		<link>http://marlongrech.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://marlongrech.wordpress.com/osd.xml" title="C# Disciples" />
		<item>
		<title>Filtering CollectionViewSource Dynamically</title>
		<link>http://marlongrech.wordpress.com/2008/02/11/filtering-collectionviewsource-dynamically/</link>
		<comments>http://marlongrech.wordpress.com/2008/02/11/filtering-collectionviewsource-dynamically/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 12:10:36 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/?p=110</guid>
		<description><![CDATA[[Please read this post before starting to read this one]
In my previous post I talked about how one can embed code in XAML using the Dynamic Expression API. I was thinking on how to expand this even more. And I figured out&#8230; What if I use this for the Filter event of the CollectionViewSource&#8230;. Well [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=110&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p><a href="http://marlongrech.wordpress.com/2008/02/10/embed-code-in-xaml/">[Please read this post before starting to read this one]</a></p>
<p>In <a href="http://marlongrech.wordpress.com/2008/02/10/embed-code-in-xaml/">my previous post</a> I talked about how one can embed code in XAML using the <a title="Dynamic LINQ" href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" target="_blank">Dynamic Expression API</a>. I was thinking on how to expand this even more. And I figured out&#8230; What if I use this for the Filter event of the CollectionViewSource&#8230;. Well I tried it out and it ended up working quite well&#8230;. What is even better is that you can allow the user to enter a string that is then compiled in a Lambda expression and applied for filtering data <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p>So basically my implementation includes an Attached Property that you can use for a CollectionViewSource. This property is called <strong>FilterExpression</strong> and is in the class called <strong>LambdaCollectionView</strong>. All you have to do is to pass a string that  is the filter expression that you want to evaluate for filtering.  As context for your expression there is a predefined variable called <strong>item </strong>that is basically the type of object that you have in the collection&#8230;. example, if you a list of strings you can treat item as a string (i.e you can use all string functions such as StartsWith etc&#8230;.)<br />
Let&#8217;s do a small example on how you can use this. Imagine you have a list of strings and you want to apply a filter that filters all strings that do not start with the character M. you would do something like this</p>
<p><a title="filtering 1" href="http://marlongrech.files.wordpress.com/2008/02/filtering1.jpg"><img src="http://marlongrech.files.wordpress.com/2008/02/filtering1.jpg" alt="filtering 1" /></a></p>
<p>As you can see here you have to use the $ symbol for strings just like in the <a title="LambdaValueConverter" href="http://marlongrech.wordpress.com/2008/02/10/embed-code-in-xaml/" target="_blank">LambdaValueConverter.</a> Ok ok&#8230; so this is cool<a title="LambdaValueConverter" href="http://marlongrech.wordpress.com/2008/02/10/embed-code-in-xaml/" target="_blank"> </a>but what is even cooler is the fact that you can make the user decide how to filter <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>In the demo app I created a textbox and a button. The user can enter an expression in the textbox and when the button is clicked I reset the FilterExpression property and bamm the filter changes&#8230; In the textbox the user does not need to use the $ symbol for strings since the textbox handles &#8221; unlike the XAML parser&#8230;.</p>
<p><a title="filtering 2" href="http://marlongrech.files.wordpress.com/2008/02/filtering2.jpg"><img src="http://marlongrech.files.wordpress.com/2008/02/filtering2.jpg" alt="filtering 2" /></a></p>
<p>This is very handy if you want to apply some basic filtering from the UI&#8230;. The filter also supports user friendly expressions like <em>Not instead of != </em>and many others.. For a complete list of supported expressions read the Dynamic Expressions.html under the Dynamic Query folder (inside the download)</p>
<p>Hope you like this and give me more feedback on the AvalonLambdas &#8230;.</p>
<p>Regards</p>
<p><a href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/WPFLambdas.zip">Download Source Code</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/110/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/110/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/110/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/110/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/110/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=110&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2008/02/11/filtering-collectionviewsource-dynamically/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/02/filtering1.jpg" medium="image">
			<media:title type="html">filtering 1</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/02/filtering2.jpg" medium="image">
			<media:title type="html">filtering 2</media:title>
		</media:content>
	</item>
		<item>
		<title>Embed code in XAML</title>
		<link>http://marlongrech.wordpress.com/2008/02/10/embed-code-in-xaml/</link>
		<comments>http://marlongrech.wordpress.com/2008/02/10/embed-code-in-xaml/#comments</comments>
		<pubDate>Sun, 10 Feb 2008 23:06:36 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.0]]></category>
		<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/?p=105</guid>
		<description><![CDATA[Loads of time I needed a converter that does a very small operation such as binding to the Height of an element and subtract 10 from the binded value. Every time you have to create a converter to do so. I must admit, it really makes me angry because I would like to this in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=105&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Loads of time I needed a converter that does a very small operation such as binding to the Height of an element and subtract 10 from the binded value. Every time you have to create a converter to do so. I must admit, it really makes me angry because I would like to this in XAML.</p>
<p>I found this cool code sample on <a title="Dynamic LINQ" href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" target="_blank">Dynamic Expression API</a> from msdn and I thought why don&#8217;t I use it to be able to embed code in XAML&#8230;. Apparently I am not the first one trying this out . While having a chat with <a title="Josh Smith" href="http://joshsmithonwpf.wordpress.com/" target="_blank">WPF Guru, Josh Smith</a>, he pointed out that someone else created something similar. <a class="url fn" title="View all posts by M. Orçun Topdağı" href="http://www.fikrimvar.net/lestirelim/?author=2">M. Orçun Topdağı</a> did a very great job in c<a href="http://www.fikrimvar.net/lestirelim/?p=15" target="_blank">reating converters</a> that can be declared in XAML. Great Job dude!!! It is really unfortunate that I found out about this after I created my own <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Anyway now that I did it might as well show it to you guys right <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So basically this is quite simple. I created a Markup Extension called <strong>LambdaValueConverter</strong> that creates an instance of a converter that accepts a LambdaExpression which is then compiled (using the <a title="Dynamic LINQ" href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" target="_blank">Dynamic Expression API</a>) and run whenever the binding for the converter changes. In order to use this <strong>LambdaValueConverter</strong> you have to pass the Lambda Expression that you want to evaluate when the binding changes. Let me give you an example. Imagine you want to bind to the Height property of a Grid and <em>divide it&#8217;s height by 2 and subtract 20</em>. You would do something like this in the Binding</p>
<p><a title="Binding Lambdas1" href="http://marlongrech.files.wordpress.com/2008/02/binding1.jpg"><img src="http://marlongrech.files.wordpress.com/2008/02/binding1.jpg" alt="Binding Lambdas1" /></a></p>
<p>As you can see this is quite easy to use&#8230; It is important to note that <strong>param</strong> is a predefined variable that holds the binding value (in the case of this example it will be a Double since we are binding to the height property of a Grid). In the expression you can write anything that is supported in the <a title="Dynamic LINQ" href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx" target="_blank">Dynamic Expression API</a>.</p>
<p>One handy feature I added, is support for strings. Imagine you want to concatenate a string with a binding value. Instead  having to create 2 TextBlocks and loads of other things, all you have to do is to enter the text you want + the <strong>param</strong> that is supplied to you by the converter. You have to surround your string with the $ symbol(don&#8217;t ask why I choose this symbol <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  ) in order for the parser to know that the specified text is a string. It would look something like this&#8230;.</p>
<p><a title="Binding Lambdas2" href="http://marlongrech.files.wordpress.com/2008/02/binding2.jpg"><img src="http://marlongrech.files.wordpress.com/2008/02/binding2.jpg" alt="Binding Lambdas2" /></a></p>
<p>So as you can imagine the output of this binding will be &gt;&gt; The border size is {the value of the binding}.</p>
<p>Cool&#8230;. Yet I wanted to go a step further. Basically I asked myself and said, &#8220;where can this fit besides for converters&#8221; and BAMM&#8230; I said what about having such a feature for the CanExecute of commands.. Usually in the can execute of a command all you do is a one line of code yet you have to create an event handler every time (same problem as converters!) So  I created a class that inherits from the CommandBinding class and extended it to support a expression string that can be evaluated as the CanExecute event handler. In order to use this all you have to do is, create an instance of the <strong>CommandLambdas </strong>class and set the <strong>CanExecuteExpression </strong>property with a string that is your expression. As context of the expression there are two predefined variables that you can use. <strong>param</strong> that is the parameter that you are passing for the command  and <strong>e</strong> that is the CanExecuteRoutedEventArgs  for the CanExecute event. Lets make an example. Imagine that you have a checkbox that determines if the Command for a button can execute or not. The code for the Button would look something like this</p>
<p><a title="Binding Lambdas3" href="http://marlongrech.files.wordpress.com/2008/02/binding3jpg.jpg"><img src="http://marlongrech.files.wordpress.com/2008/02/binding3jpg.jpg" alt="Binding Lambdas3" /></a></p>
<p>Just for the sake of the demo I used the Application.New command so that I don&#8217;t have to create my own <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  Ok, so here we are setting the checkbox as our parameter for the command. Now all we have to do is to declare our command binding. We have to declare a <strong>CommandLambdas </strong>and set the <strong>CanExecuteExpression </strong>property. Something like this&#8230;.</p>
<p><a title="Binding Lambdas4" href="http://marlongrech.files.wordpress.com/2008/02/binding4.jpg"><img src="http://marlongrech.files.wordpress.com/2008/02/binding4.jpg" alt="Binding Lambdas4" /></a></p>
<p>So what we are doing is to check if the checkbox (that is our command parameter) is checked and if it is, we set the CanExecute. The assignment of the CanExecute is being done for us implicitly by the <strong>CommandLambdas</strong> class.</p>
<p><em>Updated:</em> I found this brilliant Library called <a href="http://http//www.blendables.com/" target="_blank">Blendables</a>&#8230; in Blendables there is a very impressive feature called SimpleBinding and Complex Binding (among others) that enable you to write code in the binding in a very clean way&#8230; much more nice + powerful than what I did here&#8230;.</p>
<p>So you can do something like this in the binding</p>
<p><strong><span style="font-size:9pt;">Width=</span><span style="font-size:9pt;">&#8220;<span style="color:blue;">{blendables:EvalBinding [{Self}.Height]/2}</span>&#8220;</span></strong></p>
<p>That&#8217;s nice isn&#8217;t it&#8230;. Have a look at their <a href="http://www.blendables.com/files/folders/148/download.aspx" target="_blank">Documentation here. </a>Or even check out their samples <a href="http://www.blendables.com/blogs/blendables/archive/tags/EvalBinding/default.aspx" target="_self">over here</a>.</p>
<p>Well that is all&#8230; I am looking forward for feedback to see how we can extend even more this to be even more useful for WPF Developers <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Looking forward for your feedback.</p>
<p><a href="http://marlongrech.wordpress.com/2008/02/11/filtering-collectionviewsource-dynamically/"> See also Filterering CollectionViewSource Dynamically</a></p>
<p><strong><a title="Source Code for WPF Lambdas" href="http://cid-96f8d49aa44c79c1.skydrive.live.com/self.aspx/Public/WPFLambdas.zip" target="_blank">Download Source Code for the AvalonLambdas<br />
</a></strong></p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f02%2f10%2fembed-code-in-xaml%2f"><img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f02%2f10%2fembed-code-in-xaml%2f" border="0" alt="kick it on DotNetKicks.com" /></a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/105/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/105/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/105/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/105/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/105/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=105&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2008/02/10/embed-code-in-xaml/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/02/binding1.jpg" medium="image">
			<media:title type="html">Binding Lambdas1</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/02/binding2.jpg" medium="image">
			<media:title type="html">Binding Lambdas2</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/02/binding3jpg.jpg" medium="image">
			<media:title type="html">Binding Lambdas3</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/02/binding4.jpg" medium="image">
			<media:title type="html">Binding Lambdas4</media:title>
		</media:content>

		<media:content url="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fmarlongrech.wordpress.com%2f2008%2f02%2f10%2fembed-code-in-xaml%2f" medium="image">
			<media:title type="html">kick it on DotNetKicks.com</media:title>
		</media:content>
	</item>
		<item>
		<title>Working with Expression Trees &#8211; Part 1</title>
		<link>http://marlongrech.wordpress.com/2008/01/08/working-with-expression-trees-part-1/</link>
		<comments>http://marlongrech.wordpress.com/2008/01/08/working-with-expression-trees-part-1/#comments</comments>
		<pubDate>Tue, 08 Jan 2008 01:37:17 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2008/01/08/working-with-expression-trees-part-1/</guid>
		<description><![CDATA[So here I am at 1.00 am in the morning everyone is sleeping and finally I can enjoy some peace and quite. My brain is partially not working so I apologize if I say something stupid  
Let us start&#8230;. Why is LINQ so exciting for me? That&#8217;s a good question &#8230; Well 99% of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=80&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>So here I am at 1.00 am in the morning everyone is sleeping and finally I can enjoy some peace and quite. My brain is partially not working so I apologize if I say something stupid <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Let us start&#8230;. Why is LINQ so exciting for me? That&#8217;s a good question &#8230; Well 99% of the people that know me would probably answer &#8220;It is a new .Net feature so definitely Marlon will be more than excited about it!&#8221;. Yet my friends, let me give you the answer myself! The BIG thing about LINQ is that in C# you can stop being so <i>imperative</i> and bossy and start being more <i>declarative (aka goal oriented</i><i>). </i>So basically you express yourself by creating a query and then let the underlying provider to do all the work&#8230;.  To understand more what I mean <a href="http://www.hookedonlinq.com/LINQtoSQL5MinuteOverview.ashx" target="_blank">have a look at Linq to SQL.</a></p>
<p>So assuming that you have already seen and tried LINQ, today I will talk about how the underlying systems can read the query that you write and parse it. We will not talk about LINQ but instead we will talk about Lambdas and Expression Trees.</p>
<p>Func&lt;string, bool&gt; test = x =&gt; x.Length &gt; 10;<br />
Expression&lt;Func&lt;string, bool&gt;&gt; test2 = x =&gt; x.Length &gt; 10;</p>
<p>Ok, so you would be wondering what is the difference between these 2 statements. Well there is quite a lot of difference.<br />
When the compiler grabs the first statement, it will treat this line of code like a normal delegete, producing MSIL that can &#8220;run&#8221; the specified code. Yet for the second statement the compiler will build an Expression Tree so that who ever receives the &#8220;test2&#8243; can parse the expression tree and do some work. To have a better idea of what I am saying I opened my friend Reflector and disassembled the two statements.</p>
<p><b>1st Statement </b></p>
<p>CS$&lt;&gt;9__CachedAnonymousMethodDelegate1 = delegate (string x) {<br />
return x.Length &gt; 10;<br />
};<br />
Func&lt;string, bool&gt; test = CS$&lt;&gt;9__CachedAnonymousMethodDelegate1;</p>
<p><b>2nd Statement</b></p>
<p>Expression&lt;Func&lt;string, bool&gt;&gt; test2 = Expression.Lambda&lt;Func&lt;string, bool&gt;&gt;(Expression.GreaterThan(Expression.Property(CS$0$0000 = Expression.Parameter(typeof(string), &#8220;x&#8221;), (MethodInfo) methodof(string.get_Length)), Expression.Constant(10, typeof(int))), new ParameterExpression[] { CS$0$0000 });</p>
<p>Quite a big difference one can notice !</p>
<p>Basically the compiler has building up a set of objects that we can parse in order to do a unit of work. And that is the power of Lambda and Expression Trees</p>
<p>Now that we know that the compiler will give us an Expression tree let&#8217;s dive into some code. I will will create a method that accepts an expression and translates that expression into a URL. So for example we will have a user that wants a customer that has a name of &#8216;Marlon&#8217; and we will give him &#8216;www.blablabla.com/Customer.aspx?Name=Marlon&#8217;</p>
<p>First let&#8217;s create the Customer class.</p>
<p><a href="http://marlongrech.files.wordpress.com/2008/01/linq1.jpg" title="linq1.jpg"><img src="http://marlongrech.files.wordpress.com/2008/01/linq1.jpg" alt="linq1.jpg" /></a></p>
<p>Now that we have the class let&#8217;s create a dummy method that will eventually be our URL provider.</p>
<p><a href="http://marlongrech.files.wordpress.com/2008/01/linq2.jpg" title="linq2.jpg"><img src="http://marlongrech.files.wordpress.com/2008/01/linq2.jpg" alt="linq2.jpg" /></a></p>
<p>So the method above accepts an Expression of type Func that has a Customer generic type and a bool as return type. The method will later parse the expression to form a URL out of it.</p>
<p>Now lets&#8217; call this method to see how the code for the lambda would look like.</p>
<p><a href="http://marlongrech.files.wordpress.com/2008/01/linq3.jpg" title="linq3.jpg"><img src="http://marlongrech.files.wordpress.com/2008/01/linq3.jpg" alt="linq3.jpg" /></a></p>
<p>Great we have everything set up and now we can start having some fun parsing the Expression Tree (FINALLY). The expression that was passed to us contains a Body property. The body is a <a href="http://msdn2.microsoft.com/en-us/library/system.linq.expressions.binaryexpression.aspx" target="_blank">BinaryExpression</a> that contains a Left property a right property and a NodeType property.<br />
The Left property will contain the expression on the left of our lambda i.e x.Name<br />
The Right property will contain the expression on the right of our lambda i.e &#8220;marlon&#8221;<br />
The NodeType will contain the operation that was applied between the 2 expression (<a href="http://msdn2.microsoft.com/en-us/library/bb361179.aspx" target="_blank">this is an ExpressionType enum</a> ex. Equals, GreaterThan etc..)</p>
<p>The tree would look something like this</p>
<p><a href="http://marlongrech.files.wordpress.com/2008/01/linqimg1.jpg" title="linqimg1.jpg"><img src="http://marlongrech.files.wordpress.com/2008/01/linqimg1.jpg" alt="linqimg1.jpg" /></a></p>
<p>So what we need to do now is to cast the Left expression as a <a href="http://msdn2.microsoft.com/en-us/library/system.linq.expressions.memberexpression.aspx" target="_blank">MemberExpression</a> since we know that we have an expression that is accessing a Property of the object (Please note that we are doing this assumption to simplify the blog post in a Part 2 of this post I will discuss how one should recursivly cast this expression since you can have multiples). By doing so now we can access the name of the property that the lambda is calling i.e Name and we can put it in our URL. We can also cast the Right expression to a <a href="http://msdn2.microsoft.com/en-us/library/system.linq.expressions.constantexpression.aspx" target="_blank">ConstantExpression</a> that will give us the value that is on the right side of the expression i.e &#8220;Marlon&#8221;. Having said all this have a look at the code to get a better idea of what is happening</p>
<p><a href="http://marlongrech.files.wordpress.com/2008/01/linq4.jpg" title="linq4.jpg"><img src="http://marlongrech.files.wordpress.com/2008/01/linq4.jpg" alt="linq4.jpg" /></a></p>
<p>As you can see all we are doing in this method is to get the data from the expression tree and write a string accordingly. AGAIN I want to make this crystal clear, this implementation is quite Naive. There are infinite number of problems with this&#8230;. For example if the user passes an expression like this</p>
<p>x =&gt; x.Name == &#8220;Marlon&#8221; &amp;&amp; x.Name == &#8220;Raffaele&#8221;</p>
<p>Then we would get a nice Exception because the Left Expression would not be a Member expression anymore but it would be a BinaryExpression that contains the x.Name == &#8220;Marlon&#8221; and the Right expression would be the x.Name = &#8220;Raffaele&#8221;. Something like this.</p>
<p><a href="http://marlongrech.files.wordpress.com/2008/01/linqimg2.jpg" title="linqimg2.jpg"><img src="http://marlongrech.files.wordpress.com/2008/01/linqimg2.jpg" alt="linqimg2.jpg" /></a></p>
<p>This can go on infinitly (or maybe until the developer gets tired writing the lambda <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ) and that is why recursion is needed for parsing an expression tree. I will dive into this on my next post on Expression Trees, right now I will dive into my bed because I cannot make my eyes stay opened any longer&#8230;.</p>
<p>So guys I hope you liked the article I will be posting Part 2 where we will implement a much more complex expression parser using some recursion and god knows what  not.</p>
<p><a href="http://www.snapdrive.net/files/496756/Other%20Posts/TestLambdaPart1.rar" target="_blank">Download Sample Application &gt;&gt;</a></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/80/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/80/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/80/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/80/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/80/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=80&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2008/01/08/working-with-expression-trees-part-1/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/01/linq1.jpg" medium="image">
			<media:title type="html">linq1.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/01/linq2.jpg" medium="image">
			<media:title type="html">linq2.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/01/linq3.jpg" medium="image">
			<media:title type="html">linq3.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/01/linqimg1.jpg" medium="image">
			<media:title type="html">linqimg1.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/01/linq4.jpg" medium="image">
			<media:title type="html">linq4.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2008/01/linqimg2.jpg" medium="image">
			<media:title type="html">linqimg2.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>Let keyword when using LINQ</title>
		<link>http://marlongrech.wordpress.com/2007/12/27/let-keyword-when-using-linq/</link>
		<comments>http://marlongrech.wordpress.com/2007/12/27/let-keyword-when-using-linq/#comments</comments>
		<pubDate>Thu, 27 Dec 2007 19:42:30 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2007/12/27/let-keyword-when-using-linq/</guid>
		<description><![CDATA[I found another cool feature in LINQ (yes another one). Basically LINQ introduces a new keyword, let.
This keyword lets you declare &#8220;variables&#8221;  in your query that you can use throughout the query itself and even to project data out of the query&#8230; It would look something like this&#8230;

and as expected the result would be

Quite [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=58&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I found another cool feature in LINQ (yes another one). Basically LINQ introduces a new keyword, <font color="#0000ff">let.</font></p>
<p>This keyword lets you declare &#8220;variables&#8221;  in your query that you can use throughout the query itself and even to project data out of the query&#8230; It would look something like this&#8230;</p>
<p><a href="http://marlongrech.files.wordpress.com/2007/12/linq.jpg" title="linq.jpg"><img src="http://marlongrech.files.wordpress.com/2007/12/linq.jpg" alt="linq.jpg" /></a></p>
<p>and as expected the result would be</p>
<p><a href="http://marlongrech.files.wordpress.com/2007/12/linqoutput.jpg" title="linqoutput.jpg"><img src="http://marlongrech.files.wordpress.com/2007/12/linqoutput.jpg" alt="linqoutput.jpg" /></a></p>
<p>Quite cool&#8230;.</p>
<p>Regards</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/58/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/58/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/58/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/58/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/58/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=58&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2007/12/27/let-keyword-when-using-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2007/12/linq.jpg" medium="image">
			<media:title type="html">linq.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2007/12/linqoutput.jpg" medium="image">
			<media:title type="html">linqoutput.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>More LINQ and Deferred Execution</title>
		<link>http://marlongrech.wordpress.com/2007/12/27/more-linq-and-deferred-execution/</link>
		<comments>http://marlongrech.wordpress.com/2007/12/27/more-linq-and-deferred-execution/#comments</comments>
		<pubDate>Thu, 27 Dec 2007 16:06:58 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2007/12/27/more-linq-and-deferred-execution/</guid>
		<description><![CDATA[In my previous post I talked about LINQ and Deferred Execution &#8230; I decided to continue expanding on this topic by showing how with LINQ to Objects it&#8217;s not only the query that is not executed immediately. Lets start from the basics&#8230; When you have a query such as this one

you can also write the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=50&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In my <a href="http://marlongrech.wordpress.com/2007/12/22/linq-and-deferred-execution-of-queries/" target="_blank">previous post</a> I talked about LINQ and Deferred Execution &#8230; I decided to continue expanding on this topic by showing how with LINQ to Objects it&#8217;s not only the query that is not executed immediately. Lets start from the basics&#8230; When you have a query such as this one</p>
<p><a href="http://marlongrech.files.wordpress.com/2007/12/linq1.jpg" title="linq1.jpg"><img src="http://marlongrech.files.wordpress.com/2007/12/linq1.jpg" alt="linq1.jpg" /></a></p>
<p>you can also write the above query in this way&#8230;<br />
<a href="http://marlongrech.files.wordpress.com/2007/12/linq2.jpg" title="linq2.jpg"><img src="http://marlongrech.files.wordpress.com/2007/12/linq2.jpg" alt="linq2.jpg" /></a></p>
<p>Yet you still did not execute that query&#8230; i.e the query2 that we have in the above code still has no values in it, it only holds an object that can give the values that you would want to have (It&#8217;s just a <i>plan of execution</i>). This might sound strange yet true&#8230; WHY? It&#8217;s because of Iterators. This is very important!!! For a better understanding of how iterators work have a look at <a href="http://www.ondotnet.com/pub/a/dotnet/2004/06/07/liberty.html" target="_blank">this post.</a><br />
So basically the iterator (which is an object returned by the Where method) will give you one value from a list at a time ONLY when you request it. Once you request the value, then you can go ahead and process that value and request the next value from that list. We use this a lot for example when we create a foreach statement.</p>
<p>When you have a LINQ query the same thing would happen as if we are in a foreach statement. When you project a value from your query you did not process the whole list you only got one value at a time!!! For a better understanding of how this works I created a small demo app where I developed my own Where Iterator that prints in a console. Here is the code for this demo app.</p>
<p><a href="http://marlongrech.files.wordpress.com/2007/12/linq31.jpg" title="linq31.jpg"><img src="http://marlongrech.files.wordpress.com/2007/12/linq31.jpg" alt="linq31.jpg" /></a></p>
<p>Basically I am creating an extension method for IEnumerable&lt;int&gt; and called it MyOwnWhere that in essence does the same job of the Where extension method of .Net 3.5 with the difference that this one prints to console. The interesting part of all this, is the output in the console&#8230;</p>
<p><a href="http://marlongrech.files.wordpress.com/2007/12/linq4.jpg" title="linq4.jpg"><img src="http://marlongrech.files.wordpress.com/2007/12/linq4.jpg" alt="linq4.jpg" /></a></p>
<p>As you can see when you have a where query you will be going through the list only once. The MyOwnWhere method (by using the yield keyword which is the keyword for creating iterators) is emitting an integer at a time. Lets us try to write down the flow of execution&#8230;</p>
<p>- The foreach statement requests a value from the query<br />
- The query will start by asking the MyOwnWhere for a value<br />
- MyOwnWhere will start filtering the source list<br />
- Once an item matching the lambda function passed is found we yield that value<br />
- The body of the foreach can process that value<br />
- Once the value is processed the foreach will request the next value and this will go back to step 1 until the MyOwnWhere will stop yield values.</p>
<p>So basically everything is happening &#8220;Just in time&#8221; when you request it.  To summarize all this we can even say</p>
<p>- You request a value<br />
- The Iterator  yields that value<br />
- You process the value and request another one</p>
<p>The only instances where this does not apply is when you have things like OrderBy, Group or Joins&#8230;. When you use such methods you will need to get all values before executing the order by for example. So the Where would have to be processed fully before the order by can continue&#8230;. So if we change our code to this</p>
<p><a href="http://marlongrech.files.wordpress.com/2007/12/linq5.jpg" title="linq5.jpg"><img src="http://marlongrech.files.wordpress.com/2007/12/linq5.jpg" alt="linq5.jpg" /></a></p>
<p>than the result would be this</p>
<p><a href="http://marlongrech.files.wordpress.com/2007/12/linq6.jpg" title="linq6.jpg"><img src="http://marlongrech.files.wordpress.com/2007/12/linq6.jpg" alt="linq6.jpg" /></a></p>
<p>So here we had to process the whole list before we can order the result&#8230; No more just in time over here <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I hope that this post helps a bit more you guys to understand how LINQ to Objects actually works below the covers <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Happy new year to everyone <img src='http://s.wordpress.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Regards</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/50/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/50/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/50/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/50/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/50/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=50&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2007/12/27/more-linq-and-deferred-execution/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2007/12/linq1.jpg" medium="image">
			<media:title type="html">linq1.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2007/12/linq2.jpg" medium="image">
			<media:title type="html">linq2.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2007/12/linq31.jpg" medium="image">
			<media:title type="html">linq31.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2007/12/linq4.jpg" medium="image">
			<media:title type="html">linq4.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2007/12/linq5.jpg" medium="image">
			<media:title type="html">linq5.jpg</media:title>
		</media:content>

		<media:content url="http://marlongrech.files.wordpress.com/2007/12/linq6.jpg" medium="image">
			<media:title type="html">linq6.jpg</media:title>
		</media:content>
	</item>
		<item>
		<title>LINQ and Deferred Execution of queries</title>
		<link>http://marlongrech.wordpress.com/2007/12/22/linq-and-deferred-execution-of-queries/</link>
		<comments>http://marlongrech.wordpress.com/2007/12/22/linq-and-deferred-execution-of-queries/#comments</comments>
		<pubDate>Sat, 22 Dec 2007 18:59:31 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2007/12/22/linq-and-deferred-execution-of-queries/</guid>
		<description><![CDATA[Hello WPF Disciples,
LINQ is really really cool, I am in love &#8230; Today I discovered something about LINQ that made me think i was crazy for a while &#8230; Yet then I found out what is really happening and I felt really stupid &#8230;
When you create a query with LINQ the query is not executed [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=48&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Hello WPF Disciples,</p>
<p>LINQ is really really cool, I am in love &#8230; Today I discovered something about LINQ that made me think i was crazy for a while &#8230; Yet then I found out what is really happening and I felt really stupid &#8230;</p>
<p>When you create a query with LINQ the query is not executed until it&#8217;s really needed. For example</p>
<p><font size="2"><font color="#0000ff" size="2">int<font color="#000000" size="2">[] list = </font><font color="#0000ff" size="2">new</font><font color="#000000" size="2"> </font><font color="#0000ff" size="2">int</font><font color="#000000" size="2">[]{ 1,2,3,4,5,6,7,8,9,10 };<br />
</font><font color="#0000ff" size="2">int</font><font color="#000000" size="2"> max = 5;<br />
</font><font color="#0000ff" size="2">var</font><font color="#000000" size="2"> query = </font><font color="#0000ff" size="2">from</font><font color="#000000" size="2"> x </font><font color="#0000ff" size="2">in</font><font color="#000000" size="2"> list<br />
</font><font color="#0000ff" size="2">                         where</font><font color="#000000" size="2"> x &lt;= max<br />
</font><font color="#0000ff" size="2">                        select</font><font size="2"><font color="#000000"> x;</font></font></font></font><font size="2"><font color="#0000ff" size="2"><font size="2"><font color="#000000"> </font></font></font></font></p>
<p><font size="2"><font color="#0000ff" size="2"><font size="2"><font color="#0000ff" size="2">foreach</font><font size="2"> (</font><font color="#0000ff" size="2">var</font><font size="2"> item </font><font color="#0000ff" size="2">in</font><font size="2"> query)<br />
</font><font color="#2b91af" size="2">Console</font><font size="2">.WriteLine(item);<font color="#2b91af" size="2">Console</font><font size="2">.ReadKey(</font><font color="#0000ff" size="2">true</font><font size="2">);</font></font></font></font></font></p>
<p><font size="2"><font size="2"></font><font size="2"><font size="3"><font color="#000000"><i>The Result of this query is a print from<b> 1 &#8211; 5.</b></i><b> </b></font></font></font></font><font size="2"><b> </b></font></p>
<p><font size="2"><font color="#999999"><font size="2"><font color="#000000" size="3">The query is not executed until you reach the foreach statement. Basically LINQ will delay the execution of the query till it is really needed&#8230; This makes a lot of sense and I like the idea&#8230; BUT if you are not aware of this it can make you go crazy. For example let&#8217;s take this query and put some code that changes the state an object that is used in the query</font></font> </font></font></p>
<p><font color="#0000ff" size="2">int<font size="2">[] list = </font><font color="#0000ff" size="2">new</font><font size="2"> </font><font color="#0000ff" size="2">int</font><font size="2">[]{ 1,2,3,4,5,6,7,8,9,10 };<br />
</font><font color="#0000ff" size="2">int</font><font size="2"> max = 5;<br />
</font><font color="#0000ff" size="2">var</font><font size="2"> query = </font><font color="#0000ff" size="2">from</font><font size="2"> x </font><font color="#0000ff" size="2">in</font><font size="2"> list<br />
</font><font color="#0000ff" size="2">                         where</font><font size="2"> x &lt;= max<br />
</font><font color="#0000ff" size="2">                        select</font><font size="2"> x;<br />
</font></font><font color="#0000ff" size="2"><font size="2">max += 2;</font></font><font color="#0000ff" size="2"><br />
</font><font color="#0000ff" size="2"><font size="2"><font color="#0000ff" size="2">foreach</font><font size="2"> (</font><font color="#0000ff" size="2">var</font><font size="2"> item </font><font color="#0000ff" size="2">in</font><font size="2"> query)<br />
</font><font color="#2b91af" size="2">Console</font><font size="2">.WriteLine(item);</font></font></font><font color="#0000ff" size="2"><br />
</font><font color="#0000ff" size="2"><font size="2"><font size="2"><font color="#2b91af" size="2">Console</font><font size="2">.ReadKey(</font><font color="#0000ff" size="2">true</font><font size="2">);</font></font></font></font></p>
<p><font color="#0000ff" size="2"><font size="2"><font size="2"><font size="2"><font color="#0000ff" size="2"><font size="2"><font size="2"></font><font size="2"><font color="#000000" size="3"><i>The Result of this query is a print from<b> 1 &#8211; 7</b></i></font></font></font><font color="#000000" size="2"><b> </b></font></font></font></font></font></font></p>
<p><font color="#0000ff" size="2"><font size="2"><font size="2"><font size="2"><font color="#0000ff" size="2"><font size="2"></font><font color="#000000"><font size="2">As  you can see the result for this query is different because the <i>max </i>variable was changed before the query was executed&#8230; This can really make you go crazy if you are not aware of what is actually happening&#8230;</font><font size="2">Have a good LINQ time&#8230;.</font></font></font></font></font></font></font><font color="#0000ff" size="2"><font size="2"><font size="2"><font size="2"> </font></font></font></font><font color="#0000ff" size="2"><font size="2"><font size="2"><font size="2"><font size="2"><font size="2"><font color="#000000">Regards<font size="2"><font size="2"></font></font></font></font></font></font></font></font></font></p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/48/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/48/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/48/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/48/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/48/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=48&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2007/12/22/linq-and-deferred-execution-of-queries/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>
	</item>
		<item>
		<title>C# 3.0 new Features</title>
		<link>http://marlongrech.wordpress.com/2007/12/21/c-30-new-features/</link>
		<comments>http://marlongrech.wordpress.com/2007/12/21/c-30-new-features/#comments</comments>
		<pubDate>Fri, 21 Dec 2007 08:03:36 +0000</pubDate>
		<dc:creator>marlongrech</dc:creator>
				<category><![CDATA[.Net 3.5]]></category>
		<category><![CDATA[C# 3.0]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://marlongrech.wordpress.com/2007/12/21/c-30-new-features/</guid>
		<description><![CDATA[Hello Disciples,
I have written a new post on C# 3.0 new features on CodeProject. The article lists all new features of C# 3.0 and gives a brief description on each one of them. Please have a look at it and send me your feedback.
http://www.codeproject.com/KB/cs/csharp3.aspx
Regards
       <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=46&subd=marlongrech&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Hello Disciples,</p>
<p>I have written a new post on C# 3.0 new features on CodeProject. The article lists all new features of C# 3.0 and gives a brief description on each one of them. Please have a look at it and send me your feedback.<br />
<a href="http://www.codeproject.com/KB/cs/csharp3.aspx">http://www.codeproject.com/KB/cs/csharp3.aspx</a></p>
<p>Regards</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/marlongrech.wordpress.com/46/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/marlongrech.wordpress.com/46/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/marlongrech.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/marlongrech.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/marlongrech.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/marlongrech.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/marlongrech.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/marlongrech.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/marlongrech.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/marlongrech.wordpress.com/46/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/marlongrech.wordpress.com/46/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/marlongrech.wordpress.com/46/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=marlongrech.wordpress.com&blog=783168&post=46&subd=marlongrech&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://marlongrech.wordpress.com/2007/12/21/c-30-new-features/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/4231953dc5a7ec7d7b5acb2444eeeeab?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">C# Disciple</media:title>
		</media:content>
	</item>
	</channel>
</rss>