<?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/"
	>

<channel>
	<title>Alkampfer&#039;s Place&#187; LINQ</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet By Ricci Gian Maria</description>
	<lastBuildDate>Wed, 08 Feb 2012 17:49:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Generic wrapper for LINQ to Tree</title>
		<link>http://www.codewrecks.com/blog/index.php/2011/04/05/generic-wrapper-for-linq-to-tree/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2011/04/05/generic-wrapper-for-linq-to-tree/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 14:06:23 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2011/04/05/generic-wrapper-for-linq-to-tree/</guid>
		<description><![CDATA[In this post I dealt with a&#160; simple extension function to flatten a tree and in one of the comment Robert shared an interesting link that deal with the creation of a wrapper structure to use LINQ style function on tree structure. That article is really interesting, but the adopted solution requires to create a [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F04%2F05%2Fgeneric-wrapper-for-linq-to-tree%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2011%2F04%2F05%2Fgeneric-wrapper-for-linq-to-tree%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=LINQ&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In <a target="_blank" href="http://www.codewrecks.com/blog/index.php/2011/03/31/create-a-tree-flatten-function-with-linq/">this post</a> I dealt with a&#160; simple extension function to flatten a tree and in one of the comment <a href="http://bobbbloggg.blogspot.com/">Robert</a> shared an <a target="_blank" href="http://www.codeproject.com/KB/linq/LinqToTree.aspx">interesting link</a> that deal with the creation of a wrapper structure to use <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> style function on tree structure. That article is really interesting, but the adopted solution requires to create a wrapper for every structure you need to iterate into and I decided to spend a couple of minutes to verify how difficult is writing a generic solution.</p>
<p>Thanks to Func&lt;T&gt; is quite easy to write a wrapper that does not relay on code generation to wrap a specific tree type.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> TreeWrapper&lt;T&gt; <span style="color: #0000ff">where</span> T : <span style="color: #0000ff">class</span> </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>  {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>      <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> T _node;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>      <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> Func&lt;T, IEnumerable&lt;T&gt;&gt; _getChildernFunc;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>      <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> Func&lt;T, T&gt; _getParentFunc;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>      <span style="color: #0000ff">public</span> TreeWrapper(</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>          T node,  </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>          Func&lt;T, IEnumerable&lt;T&gt;&gt; getChildernFunc,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>          Func&lt;T, T&gt; getParentFunc)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>      {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>          _node = node;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>          _getChildernFunc = getChildernFunc;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>          _getParentFunc = getParentFunc;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>      }</pre>
<p><!--CRLF--></div>
</div>
<p>As you can see I created a wrapper that takes a node, and a couple of functions, one to find all child of the element, and the other to find the parent of the node. With this simple wrapper writing a Descendants() function is supereasy.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> IEnumerable&lt;T&gt; Descendants()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>  {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>      <span style="color: #0000ff">return</span> Descendants(_getChildernFunc(_node));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>  }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>  <span style="color: #0000ff">private</span> IEnumerable&lt;T&gt; Descendants(IEnumerable&lt;T&gt; elements)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>  {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>      <span style="color: #0000ff">return</span> elements.Concat(</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>          elements.SelectMany(e =&gt; Descendants(_getChildernFunc(e))));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>  }</pre>
<p><!--CRLF--></div>
</div>
<p>Et voilÃ , this simple method makes this <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> pass.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> [Test]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> VerifyGenericTreeNodeDescendants()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     GenericTreeNode r1 = <span style="color: #0000ff">new</span> GenericTreeNode(1,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>             <span style="color: #0000ff">new</span> GenericTreeNode(2,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>                         <span style="color: #0000ff">new</span> GenericTreeNode(4)),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>             <span style="color: #0000ff">new</span> GenericTreeNode(3),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>             <span style="color: #0000ff">new</span> GenericTreeNode(5,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>                <span style="color: #0000ff">new</span> GenericTreeNode(6), <span style="color: #0000ff">new</span> GenericTreeNode(7)));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>     var w = TreeWrapper.Create(r1, n =&gt; n.GenericTreeNodes, n =&gt; n.Parent);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>     w.Descendants().Select(e =&gt; e.Id)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>         .Should().Have.SameSequenceAs(<span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[] {2, 3, 5, 4, 6, 7});</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The GenericTreeNode class is used only for <a title="testing" href="http://www.codewrecks.com/blog/index.php/category/programming/testing/">testing</a> purpose and is a simple class that maintains a reference to parent and a collection of childs. The ancestors function is even simplier.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> IEnumerable&lt;T&gt; Ancestors()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     T parent = _getParentFunc(_node);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     <span style="color: #0000ff">while</span> (parent != <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>         <span style="color: #0000ff">yield</span> <span style="color: #0000ff">return</span> parent;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>         parent = _getParentFunc(parent);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Simply iterate to all parents until the parent is null (reached the root node), but we can still add more interesting methods.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> IEnumerable&lt;T&gt; ElementBeforeSelf()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     T parent = _getParentFunc(_node);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     <span style="color: #0000ff">if</span> (parent != <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>         <span style="color: #0000ff">return</span> _getChildernFunc(parent)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>             .TakeWhile(e =&gt; e != _node);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> T[] {};</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span> }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span> <span style="color: #0000ff">public</span> IEnumerable&lt;T&gt; ElementAfterSelf()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>     T parent = _getParentFunc(_node);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>     <span style="color: #0000ff">if</span> (parent != <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>         <span style="color: #0000ff">return</span> _getChildernFunc(parent)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>             .SkipWhile(e =&gt; e != _node)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>             .Skip(1);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21">  21:</span>     <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> T[] { };</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22">  22:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>ElementsBeforeSelft() and ElementsAfterSelf() return all sibling elements (node at same level) before and after the current node element, this makes this <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> pass.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> [Test]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> VerifyGenericTreeElementBeforeSelf()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     GenericTreeNode node;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>    <span style="color: #0000ff">new</span> GenericTreeNode(1,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>                 <span style="color: #0000ff">new</span> GenericTreeNode(2,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>                             <span style="color: #0000ff">new</span> GenericTreeNode(4)),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>                 <span style="color: #0000ff">new</span> GenericTreeNode(3),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>                 node = <span style="color: #0000ff">new</span> GenericTreeNode(5,</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>                     <span style="color: #0000ff">new</span> GenericTreeNode(6), <span style="color: #0000ff">new</span> GenericTreeNode(7)),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>                  <span style="color: #0000ff">new</span> GenericTreeNode(8),</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>                  <span style="color: #0000ff">new</span> GenericTreeNode(9));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>    var w = TreeWrapper.Create(node, n =&gt; n.GenericTreeNodes, n =&gt; n.Parent);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>     w.ElementBeforeSelf().Select(e =&gt; e.Id)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>         .Should().Have.SameSequenceAs(<span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[] { 2, 3 });</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>     w.ElementAfterSelf().Select(e =&gt; e.Id)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span>         .Should().Have.SameSequenceAs(<span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[] { 8, 9 });</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20">  20:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The only disadvantage of this technique, is that Iâ€™m not able to write something like this.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> w.Descendants().Single(e =&gt; e.Id == 5)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     .ElementBeforeSelf().Select(e =&gt; e.Id)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     .Should().Have.SameSequenceAs(<span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[] { 2, 3 });</pre>
<p><!--CRLF--></div>
</div>
<p>This query simple select a single element and then iterates on all the element before self, but it does not compile, because the Descendants() method returns an IEnumerable&lt;T&gt; (T is the wrapped object) and not a IEnumerable&lt;TreeWrapper&lt;T&gt;&gt;. You could modify the Descendants() function to wrap each returned element, but this is not optimal, so I prefer a simply extension method to rewrap an element, that permits me to write this test.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> w.Descendants().Single(e =&gt; e.Id == 5)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span>     .TreeWrap(n =&gt; n.GenericTreeNodes, n =&gt; n.Parent)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     .ElementBeforeSelf().Select(e =&gt; e.Id)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     .Should().Have.SameSequenceAs(<span style="color: #0000ff">new</span> <span style="color: #0000ff">int</span>[] { 2, 3 });</pre>
<p><!--CRLF--></div>
</div>
<p>This syntax is not bad, the Single <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> operator permits me to iterate in all Descendants node of the wrapped node, then I need to rewrap again the result to use the ElementBeforeSelf method to use again a function of the TreeWrapper class. </p>
<p>Alk.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2011/04/05/generic-wrapper-for-linq-to-tree/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linq provider via IEnumerable</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/11/20/linq-provider-via-ienumerable/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/11/20/linq-provider-via-ienumerable/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 17:32:10 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/11/20/linq-provider-via-ienumerable/</guid>
		<description><![CDATA[If you ever tried to implement a linq provider, you know that this is not a simple task. This is true for a full linq provider, but sometimes we need only a basic support, and in these situations there is probably a simpler approach. Suppose you need to give LINQ support to find user in [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F11%2F20%2Flinq-provider-via-ienumerable%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F11%2F20%2Flinq-provider-via-ienumerable%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=LINQ&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you ever tried to implement a <a target="_blank" title="linq" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">linq</a> provider, you know that this is not a simple <a href="http://blogs.msdn.com/mattwar/pages/linq-links.aspx">task</a>. This is true for a full <a title="linq" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">linq</a> provider, but sometimes we need only a basic support, and in these situations there is probably a simpler approach. </p>
<p>Suppose you need to give <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/tag/linq/">LINQ</a> support to find user in Active Directory via LDAP, you need to make query for the various properties of the User object, and you need also to make change to some of these properties and propagate those changes back in the AD. Sounds complicated? Maybe not.</p>
<p>Look at this class.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:6d7c8ba9-0858-4af5-9767-b1ce01d44594" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> ADUser
{
    System.DirectoryServices.DirectoryEntry userDirEntry </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> DirectoryEntry root;
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> ADUser(DirectoryEntry userDirEntry, DirectoryEntry root)
    {
        </span><span style="color: #0000FF;">this</span><span style="color: #000000;">.root </span><span style="color: #000000;">=</span><span style="color: #000000;"> root;
        </span><span style="color: #0000FF;">this</span><span style="color: #000000;">.userDirEntry </span><span style="color: #000000;">=</span><span style="color: #000000;"> userDirEntry;
    }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> String Description
    {
        </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> (String)userDirEntry.Properties[</span><span style="color: #800000;">"</span><span style="color: #800000;">description</span><span style="color: #800000;">"</span><span style="color: #000000;">].Value; }
        </span><span style="color: #0000FF;">set</span><span style="color: #000000;"> { userDirEntry.Properties[</span><span style="color: #800000;">"</span><span style="color: #800000;">description</span><span style="color: #800000;">"</span><span style="color: #000000;">].Value </span><span style="color: #000000;">=</span><span style="color: #000000;"> value; }
    }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> String UserName
    {
        </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> { </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> (String)userDirEntry.Properties[</span><span style="color: #800000;">"</span><span style="color: #800000;">samaccountname</span><span style="color: #800000;">"</span><span style="color: #000000;">].Value; }

    }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Update()
    {
        userDirEntry.CommitChanges();
    }

    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> ADGroupCollection Groups { </span><span style="color: #0000FF;">get</span><span style="color: #000000;">; </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">set</span><span style="color: #000000;">; }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>As you can see, this is a simple object that wraps a DirectoryEntry related to an ActiveDirectory user, it wraps only Name and Description, but it can be updated to support all user properties. Now I create another simply class.</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8c368ff1-916f-43fc-8b9a-54f58c27ace3" class="wlWriterEditableSmartContent">
<pre style="background-color:#FFFFFF;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> ADUsers : IEnumerable</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">ADUser</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">
{
    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> IList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">ADUser</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> Users;
    System.DirectoryServices.DirectoryEntry root </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> ADUsers(System.DirectoryServices.DirectoryEntry root)
    {
        </span><span style="color: #0000FF;">this</span><span style="color: #000000;">.root </span><span style="color: #000000;">=</span><span style="color: #000000;"> root;
        Users </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">ADUser</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();
        DirectorySearcher search </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> DirectorySearcher(root);
        search.Filter </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">(&amp;(objectClass=user)(objectCategory=person))</span><span style="color: #800000;">"</span><span style="color: #000000;">;
        search.PropertiesToLoad.Add(</span><span style="color: #800000;">"</span><span style="color: #800000;">cn</span><span style="color: #800000;">"</span><span style="color: #000000;">);
        SearchResultCollection results </span><span style="color: #000000;">=</span><span style="color: #000000;"> search.FindAll();
        </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (SearchResult result </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> results)
        {
            var de </span><span style="color: #000000;">=</span><span style="color: #000000;"> result.GetDirectoryEntry();
            Users.Add(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> ADUser(de, root));
        }
    }
    </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> IEnumerator</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">ADUser</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> GetEnumerator()
    {
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> Users.GetEnumerator();
    }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> GetEnumerator();
    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is a real simple classes, it accepts a DirectoryEntry root used to query the Ldap, then it retrieves all the users object with a simple LDap query issued by DirectorySearcher object, for each result, it builds a wrapper ADuser. Now that we have these two simple classes we can write.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/11/image19.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/11/image_thumb19.png" width="640" height="144" /></a> </p>
<p>Since I loaded in memory a wrapper for each object, I can issue <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/tag/linq/">LINQ</a> queries against the AdUsers, because I can rely on the support of Linq 2 Object, simply making AdUsers implementing IEnumerable. Since everything is in memory we can issue condition like u.UserName.Contains(â€œampâ€) or whatever you like.</p>
<p>Surely this is not the optimal solution, but is quick and works well. The main drawback is that all Users needs to be loaded into memory, so we are using more resources respect a full LINQ provider, that can analyze the expression and retrieve only users that satisfies the query. But since we are not expecting Thousands of users, this can be a viable KISS solution.</p>
<p>This simple example shows that, to support LINQ for some source, sometimes there is no reason to pass for the full <a title="IQueryable" href="http://www.codewrecks.com/blog/index.php/2008/03/04/iqueryable/">IQueryable</a>&lt;T&gt;.</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/LINQ" rel="tag">LINQ</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/11/20/linq-provider-via-ienumerable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Manipulate Expression Tree in DtoGenerator</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/08/12/manipulate-expression-tree-in-dtogenerator/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/08/12/manipulate-expression-tree-in-dtogenerator/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 09:06:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software Architecture]]></category>
		<category><![CDATA[ExpressionTree]]></category>
		<category><![CDATA[T4 Generator]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/08/12/manipulate-expression-tree-in-dtogenerator/</guid>
		<description><![CDATA[Iâ€™m writing a simple Dto generator, and today I found a challenging problem. I supported dto composition like this: I have a CustomerDto3 that have only CustomerId and ContactName properties, then I want to autogenerate a OrderTestDto that have a Customers property of type CustomerDto3. The syntax on My T4 generator is this one. SyntaxCode [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F08%2F12%2Fmanipulate-expression-tree-in-dtogenerator%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F08%2F12%2Fmanipulate-expression-tree-in-dtogenerator%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=ExpressionTree,LINQ,T4+Generator&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Iâ€™m writing a simple <a href="http://www.codewrecks.com/blog/index.php/2009/07/31/dto-generator-and-repository-integration/">Dto generator</a>, and today I found a challenging problem. I supported dto composition like this:</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2009/08/image19.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="265" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2009/08/image-thumb19.png" width="450" border="0" /></a> </p>
<p>I have a CustomerDto3 that have only CustomerId and ContactName properties, then I want to autogenerate a OrderTestDto that have a Customers property of type CustomerDto3. The syntax on My <a title="T4 generator" href="http://www.codewrecks.com/blog/index.php/tag/t4-generator/">T4 generator</a> is this one.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:8f382282-83e3-4c14-9b64-466fb66b8333" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">SyntaxCode syntax </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> SyntaxCode(
    </span><span style="color: #800000;">@"</span><span style="color: #800000;">binDebugDtoFactory.Exe</span><span style="color: #800000;">"</span><span style="color: #000000;">,
    Path.GetDirectoryName(Host.TemplateFile),
    </span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory.Dto</span><span style="color: #800000;">"</span><span style="color: #000000;">,
    </span><span style="color: #800000;">"</span><span style="color: #800000;">http://dtonamespace.org</span><span style="color: #800000;">"</span><span style="color: #000000;">);   

syntax.Render(
    </span><span style="color: #800000;">"</span><span style="color: #800000;">DtoFactory.Orders[OrdersTestDto] (OrderID,OrderDate,Customers) (Customers|CustomerDto3)</span><span style="color: #800000;">"</span><span style="color: #000000;">,
    </span><span style="color: #800000;">"</span><span style="color: #800000;">Generated\OrderDto3.cs</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Iâ€™ve simplified dto creation, and supported generation of each dto in a different file. The main problem supporting this scenario is <em>how to write the assembler of the container class</em>. Iâ€™ve started generating this code.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:24e2be1c-4dd6-4411-b9c1-2fd6045eaa70" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Orders, OrdersTestDto</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;"> ExpressionSelector;

</span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Assembler() {

ExpressionSelector </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> OrdersTestDto() {
        OrderDate </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.OrderDate,
        OrderID </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.OrderID,
        Customers </span><span style="color: #000000;">=</span><span style="color: #000000;"> CustomerDto3.Assembler.FromOriginal(obj.Customers),
        }; </span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>It works perfectly, after all since all the Dto are generated by the same generator, when I have to generate code that transforms an original Orders object into an OrdersTestDto, I know that the creation of CustomerDto3 can be delegated to the assembler of CustomerDto3 object. This is good but now this code wont work.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:c9fed9d7-dca5-49d2-9dd9-b76f37d2b790" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
    {
        var Query </span><span style="color: #000000;">=</span><span style="color: #000000;"> context.Orders
            .Where(o </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> o.Customers.CustomerID </span><span style="color: #000000;">==</span><span style="color: #000000;"> </span><span style="color: #800000;">"</span><span style="color: #800000;">ALFKI</span><span style="color: #800000;">"</span><span style="color: #000000;">)
            .Select(OrdersTestDto.Assembler.ExpressionSelector);</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>If you read my older article, you find that one the most useful feature of dto generator, is the possibility to use expressions to make a projection in the database, but with previous generated Expression, <a target="_blank" title="Entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">Entity</a> Framework give me this error</p>
<p><div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0f0a5cc7-f93b-4f84-a095-076108f78fba" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">failed: System.NotSupportedException : <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> to Entities does not recognize the method </span><span style="color: #800000;">'</span><span style="color: #800000;">DtoFactory.Dto.CustomerDto3 FromOriginal(</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This is obvious, because when EF translate the <a title="ExpressionTree" href="http://www.codewrecks.com/blog/index.php/tag/expressiontree/">ExpressionTree</a> into SQL code, it finds a call to user function, and he cannot know how to proceed. This seems to me a really bad limitation, so I decided to fix it. The problem is that the expression should be generated this way.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:3460fb86-048d-4cd5-a106-637cd695b59b" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Orders, OrdersTestDto</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;"> Expression2Selector
    </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> OrdersTestDto()
      {
          OrderDate </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.OrderDate,
          OrderID </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.OrderID,
          Customers </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> CustomerDto3()
          {
            CustomerID </span><span style="color: #000000;">=</span><span style="color: #000000;">  obj.Customers.CustomerID,
            ContactName </span><span style="color: #000000;">=</span><span style="color: #000000;"> obj.Customers.ContactName,
          },
      };</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>With such an expression the EF provider is able to do the projection, but now a big problem arise. Iâ€™m working with T4 code generation, when the generator of OrdersTestDto has to generate the Expression, he does not know how the CustomerDto3 was generated, so he cannot generate such an expression. I really want to avoid the need to find a way to make different generators to share data.</p>
<p> After a brief thinking I realize that I already have the right expression built in CustomerDto3, so the only stuff I need is to compose the two expression, but this can be more complex than you can think. After lot of experiments, I came to this little trick.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:486f65e3-a78d-4ba4-88df-db5dc34645fe" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> ParseSelector()
{
    ExpressionSelector </span><span style="color: #000000;">=</span><span style="color: #000000;"> (Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Orders, OrdersTestDto</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;">)
        </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> DtoExpressionVisitor().ReplaceDtoBinding(ExpressionSelector);
}

</span><span style="color: #0000FF;">static</span><span style="color: #000000;"> Assembler() {

    ExpressionSelector </span><span style="color: #000000;">=</span><span style="color: #000000;"> ...
    ParseSelector();
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>The trick works in this way, I generate the ExpressionSelector in the usual way, with the call to FromOriginal that does not work in EF <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> query, but in the static constructor I call a ParseSelector function that rewrite the expression tree making it works with <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/tag/linq/">LINQ</a> query <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> . Here is the full code of the DtoExpressionVisitor().</p>
<div class="wlWriterEditableSmartContent" id="scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:eecf8639-fa23-4d75-97f9-cc082545deea" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre class="brush: csharp; gutter: true; first-line: 1; tab-size: 4;  toolbar: false; ">class DtoExpressionVisitor : ExpressionVisitor
{
	private ParameterExpression parameter;
	public Expression ReplaceDtoBinding(LambdaExpression exp)
	{
		parameter = exp.Parameters[0];
		return this.Visit(exp);
	}

	protected override System.<a title="Linq" href="http://www.codewrecks.com/blog/index.php/tag/linq/">Linq</a>.Expressions.MemberBinding VisitBinding(System.Linq.Expressions.MemberBinding binding)
	{
		if (binding is MemberAssignment)
		{
			MemberAssignment assign = (MemberAssignment)binding;
			if (assign.Expression is MethodCallExpression)
			{
				MethodCallExpression callex = (MethodCallExpression)assign.Expression;
				if (callex.Arguments.Count == 1)
				{
					MemberExpression argument = (MemberExpression) callex.Arguments[0];
					Type assembler = callex.Method.DeclaringType;
					if (assembler != null)
					{
						//The object is a dto and it is assemblable.
						FieldInfo expSelector = assembler.GetField(&quot;ExpressionSelector&quot;, BindingFlags.Static | BindingFlags.Public);
						if (expSelector != null)
						{
							LambdaExpression lambda = (LambdaExpression)expSelector.GetValue(null);
							MemberInitExpression minit = (MemberInitExpression)lambda.Body;
							List&lt;MemberBinding&gt; bindings = new List&lt;MemberBinding&gt;();
							foreach (MemberBinding oribind in minit.Bindings)
							{
								//1&#194;&#176; step. access obj.Property
								MemberExpression accessOriObj = Expression.Property(parameter, argument.Member.Name);
								//2&#194;&#176; step. access obj.Property.p'roperty
								MemberExpression accessObjProperty = Expression.Property(accessOriObj, oribind.Member.Name);
								//3&#194;&#176; recreate the binding and store into a list
								MemberBinding newPropertyBind = Expression.Bind(oribind.Member, accessObjProperty);
								bindings.Add(newPropertyBind);
							}
							NewExpression constructor = Expression.New(((PropertyInfo) assign.Member).PropertyType);
							MemberInitExpression newInitExpression = Expression.MemberInit(constructor, bindings.ToArray());
							MemberBinding newBindingExpression = Expression.Bind(assign.Member, newInitExpression);
							return newBindingExpression;
						}
					}
				}
			}
		}
		return base.VisitBinding(binding);
	}</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is quite complicated code, and it works by replacing part of the original expression tree thus composing two expression tree into one. All initial conditions are needed to identify nodes that need correction, I need to correct all MemberBinding node that are MethodCallExpression, because those ones are the ones calling the FromOriginal function from the other dto. When I find a node to change, I otbain with reflection the original expression selector (a lambdaexpression), where the body is the one used to initialize the Dto. Now I have to regenerate a similar expression using the parameter of the original lambda. Basically the problem is the following, the OrdersTestDto expressionSelector has a parameter named obj of type orders, but the ExpressionSelector of CustomersDto3 has one parameter named obj but of type Customers. This means that I need to regenerate the whole <a title="ExpressionTree" href="http://www.codewrecks.com/blog/index.php/tag/expressiontree/">ExpressionTree</a> part, changing how parameters works.</p>
<p>The first step is to regenerate all MemberBinding expression part, with the original parameter (line 34-38) and storing them into a collection (line 39). The operation is the following, I need a MemberExpression that extract from the parameter the object needed to generate the Dto. In the above example accessing the Customers property of Orders to gets a Customer object. Then another MemberExpression to take the corresponding field from the Customers object, and finally a MemberBinding. </p>
<p>Then I need to recreate the BindingExpression (line 41-43) and returning in place of the original one. All the code is based on the <a target="_blank" href="class DtoExpressionVisitor : ExpressionVisitor">original Tree Visitor by microsoft</a>. With this quite complex trick Iâ€™m able to change the expression at runtime, thus keeping the generator simple.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/T4 Generator" rel="tag">T4 Generator</a> <a target="_blank" href="http://technorati.com/tag/Expression Tree" rel="tag">Expression Tree</a> <a target="_blank" href="http://technorati.com/tag/LINQ" rel="tag">LINQ</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/08/12/manipulate-expression-tree-in-dtogenerator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Expression tree to the rescue</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/07/27/expression-tree-to-the-rescue/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/07/27/expression-tree-to-the-rescue/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 16:41:45 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[.NET framework]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[ExpressionTree; LINQ]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/07/27/expression-tree-to-the-rescue/</guid>
		<description><![CDATA[I have a little validation library that must support localized error messages, it uses a simple techniques, when you set an error message for a specific validator, if you do not want localization you can simply pass a single string, but if you want message to be localized, you specified two strings, one is the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F27%2Fexpression-tree-to-the-rescue%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F07%2F27%2Fexpression-tree-to-the-rescue%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=ExpressionTree%3B+LINQ&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I have a little validation library that must support localized error messages, it uses a simple techniques, when you set an error message for a specific validator, if you do not want localization you can simply pass a single string, but if you want message to be localized, you specified two strings, one is the name of the resource that contains the message and the other is the key of the message.</p>
<p>All this logic is inserted into a class called ErrorMessage that was created at the time of .NET 2.0; here is a typical <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a></p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:52a04c9a-aea0-4387-b32d-284fed37109f" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">[Test]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> TestLocalizationIt()
{
    ErrorMessage sut </span><span style="color: #000000;">=</span><span style="color: #000000;">
        </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> ErrorMessage(</span><span style="color: #800000;">"</span><span style="color: #800000;"><a title="Test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">Test</a></span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">DotNetMarche.Validator.Tests.ResourcesFiles.TestRes, DotNetMarche.Validator.Tests</span><span style="color: #800000;">"</span><span style="color: #000000;">);
    Assert.That(sut.ToString(System.Globalization.CultureInfo.CreateSpecificCulture(</span><span style="color: #800000;">"</span><span style="color: #800000;">It-It</span><span style="color: #800000;">"</span><span style="color: #000000;">)), Is.EqualTo(</span><span style="color: #800000;">"</span><span style="color: #800000;">Questa Ã¨ una stringa di test</span><span style="color: #800000;">"</span><span style="color: #000000;">));
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This is far from being usable, because the needs to specify the full name of the resource file that contains resources is not usable. The user can mistype the Resource File name and introduce errors that will be discovered only at runtime,&#160; and only when an object does not pass validation. At that time it was acceptable, we included all error messages into a resource file, then Store the name of the resource file into a constant and all went good. </p>
<p>Now with Expression tree we can obtain more. First of all notice that when you include a .resx file into a solution <a target="_blank" title="visual studio" href="http://blogs.msdn.com/b/visualstudio/">visual studio</a> creates for you a code behind file to easy the usage of resources.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:a2668c2f-9d09-4e5b-a5d7-fca1ca8967a5" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">[</span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.CodeDom.Compiler.GeneratedCodeAttribute(</span><span style="color: #800000;">"</span><span style="color: #800000;">System.Resources.<a title="Tools" href="http://www.codewrecks.com/blog/index.php/tag/tools/">Tools</a>.StronglyTypedResourceBuilder</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">2.0.0.0</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
[</span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[</span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
</span><span style="color: #0000FF;">internal</span><span style="color: #000000;"> </span><span style="color: #0000FF;">class</span><span style="color: #000000;"> TestRes {

    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.Resources.ResourceManager resourceMan;

    </span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.Globalization.CultureInfo resourceCulture;

    [</span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.Diagnostics.<a title="CodeAnalysis" href="http://www.codewrecks.com/blog/index.php/tag/codeanalysis/">CodeAnalysis</a>.SuppressMessageAttribute(</span><span style="color: #800000;">"</span><span style="color: #800000;">Microsoft.<a title="Performance" href="http://www.codewrecks.com/blog/index.php/tag/performance/">Performance</a></span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #800000;">"</span><span style="color: #800000;">CA1811:AvoidUncalledPrivateCode</span><span style="color: #800000;">"</span><span style="color: #000000;">)]
    </span><span style="color: #0000FF;">internal</span><span style="color: #000000;"> TestRes() {
    }

    </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span><span style="color: #008000;">
    </span><span style="color: #808080;">///</span><span style="color: #008000;">   Returns the cached ResourceManager instance used by this class.
    </span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span><span style="color: #808080;">
</span><span style="color: #000000;">    [</span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.ComponentModel.EditorBrowsableAttribute(</span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.ComponentModel.EditorBrowsableState.Advanced)]
    </span><span style="color: #0000FF;">internal</span><span style="color: #000000;"> </span><span style="color: #0000FF;">static</span><span style="color: #000000;"> </span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.Resources.ResourceManager ResourceManager {
        </span><span style="color: #0000FF;">get</span><span style="color: #000000;"> {
            </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #0000FF;">object</span><span style="color: #000000;">.ReferenceEquals(resourceMan, </span><span style="color: #0000FF;">null</span><span style="color: #000000;">)) {
                </span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.Resources.ResourceManager temp </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> </span><span style="color: #0000FF;">global</span><span style="color: #000000;">::System.Resources.ResourceManager(</span><span style="color: #800000;">"</span><span style="color: #800000;">DotNetMarche.Validator.Tests.ResourcesFiles.TestRes</span><span style="color: #800000;">"</span><span style="color: #000000;">, </span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(TestRes).Assembly);
                resourceMan </span><span style="color: #000000;">=</span><span style="color: #000000;"> temp;
            }
            </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> resourceMan;
        }
    }</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>You can see that this object has an internal property called <strong>ResouceManager</strong> that is <em>static and contains the resource manager used to access resource file</em>. Now the goal is to make this syntax possible.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:661dbd37-ac55-475b-a19b-da01f1b12107" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #000000;">[Test]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> TestLocalizationItFullFluentExplicitCulture()
{
    ErrorMessage sut </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> ErrorMessage(() </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> TestRes.Test);
    Thread.CurrentThread.CurrentUICulture </span><span style="color: #000000;">=</span><span style="color: #000000;"> System.Globalization.CultureInfo.CreateSpecificCulture(</span><span style="color: #800000;">"</span><span style="color: #800000;">En-Us</span><span style="color: #800000;">"</span><span style="color: #000000;">);
    Assert.That(sut.ToString(System.Globalization.CultureInfo.CreateSpecificCulture(</span><span style="color: #800000;">"</span><span style="color: #800000;">It</span><span style="color: #800000;">"</span><span style="color: #000000;">)), Is.EqualTo(</span><span style="color: #800000;">"</span><span style="color: #800000;">Questa Ã¨ una stringa di test</span><span style="color: #800000;">"</span><span style="color: #000000;">));
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This test create the ErrorMessage object with a lambda, now you can have full intellisense and you cannot mistype anything, in the test I set the currentUICulture to En-us and call ToString() of the ErrorMessage asking for Italian culture, to verify that I can really specify culture different from the CurrentUICulture. </p>
<p>The constructor that permits me to obtain this is the following.</p>
<div class="wlWriterEditableSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:e9a84e16-c0ca-40cd-afb9-36c50acde6b6" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;"><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;summary&gt;</span><span style="color: #008000;">
</span><span style="color: #808080;">///</span><span style="color: #008000;"> Basic Constructor
</span><span style="color: #808080;">///</span><span style="color: #008000;"> </span><span style="color: #808080;">&lt;/summary&gt;</span><span style="color: #808080;">
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> ErrorMessage(Expression</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">String</span><span style="color: #000000;">&gt;&gt;</span><span style="color: #000000;"> messageLambda)
{
    MemberExpression me </span><span style="color: #000000;">=</span><span style="color: #000000;"> messageLambda.Body </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> MemberExpression;
    Type t </span><span style="color: #000000;">=</span><span style="color: #000000;"> me.Member.DeclaringType;
    </span><span style="color: #008000;">//</span><span style="color: #008000;">now I know that this type is one generated by the <a target="_blank" title="visual studio" href="http://blogs.msdn.com/b/visualstudio/">visual studio</a></span><span style="color: #008000;">
</span><span style="color: #000000;">    PropertyInfo pinfo </span><span style="color: #000000;">=</span><span style="color: #000000;"> t.GetProperty(</span><span style="color: #800000;">"</span><span style="color: #800000;">ResourceManager</span><span style="color: #800000;">"</span><span style="color: #000000;">, BindingFlags.Static </span><span style="color: #000000;">|</span><span style="color: #000000;"> BindingFlags.NonPublic);
    mMessage </span><span style="color: #000000;">=</span><span style="color: #000000;"> me.Member.Name;
    mResourceTypeName </span><span style="color: #000000;">=</span><span style="color: #000000;"> t.FullName;
    </span><span style="color: #0000FF;">lock</span><span style="color: #000000;"> (mResMangaers)
    {
        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (</span><span style="color: #000000;">!</span><span style="color: #000000;">mResMangaers.ContainsKey(t.FullName))
        {
            mResMangaers.Add(t.FullName, pinfo.GetValue(</span><span style="color: #0000FF;">null</span><span style="color: #000000;">, </span><span style="color: #0000FF;">null</span><span style="color: #000000;">));
        }

    }
}</span></pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>My object was designed to store all resource managers into a static hashtable, to minimize memory usage, so I simply declare this constructor that accepts an Expression&lt;Func&lt;String&gt;&gt;, then I know that I have a <strong>MemberExpression</strong>, because the user specify one of the autogenerated properties of the .resx file, then with a little bit of reflection I grab the ResourceManager and the game is done.</p>
<p>With Expression tree you can quite always avoid the need for the user of you library to specify object by name if you need to use reflection.</p>
<p>alk.</p>
<div class="wlWriterEditableSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:9af0f1e3-60a5-4aa4-b8d7-ae3e0c45d52b" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><a target="_blank" title="Technorati Tags" href="http://www.codewrecks.com/blog/index.php/2008/04/22/technorati-tags/">Technorati Tags</a>: <a href="http://technorati.com/tags/Expression+Tree" rel="tag">Expression Tree</a>,<a target="_blank" href="http://technorati.com/tags/.NET+framework" rel="tag">.NET framework</a></div>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/07/27/expression-tree-to-the-rescue/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Entity Framework first steps</title>
		<link>http://www.codewrecks.com/blog/index.php/2009/01/08/entity-framework-first-steps/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2009/01/08/entity-framework-first-steps/#comments</comments>
		<pubDate>Thu, 08 Jan 2009 18:38:45 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2009/01/08/entity-framework-first-steps/</guid>
		<description><![CDATA[For those used to ORM like NHibernate, the approach to Entity Framework is quite simple, but I&#8217;ve noticed that for people that never heard of ORM in general, EF tends to create some confusion. I see people that make confusion between LINQ and EF, LINQ is the Language Integrated Query, it can be used to [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F01%2F08%2Fentity-framework-first-steps%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2009%2F01%2F08%2Fentity-framework-first-steps%2F&amp;source=alkampfer&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>For those used to ORM like <a target="_blank" title="NHibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">NHibernate</a>, the approach to <a title="Entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">Entity</a> Framework is quite simple, but I&#8217;ve noticed that for people that never heard of ORM in <a title="general" href="http://www.codewrecks.com/blog/index.php/category/general/">general</a>, EF tends to create some confusion.</p>
<p>I see people that make confusion between <a target="_blank" title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> and EF, <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> is the Language Integrated Query, it can be used to query objects in memory, XML, <a title="NHibernate" href="http://www.codewrecks.com/blog/index.php/category/frameworks/nhibernate/">NHibernate</a> and of course EF, but is not the only way to query EF. A less known method to execute query against a EF context is <a href="http://msdn.microsoft.com/en-us/library/bb387145.aspx">EntitySql</a>, here is an example.</p>
<p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:53cc3810-4e47-40b1-bb26-5bc95210fc10" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
{
    ObjectQuery query </span><span style="color: #000000;">=</span><span style="color: #000000;"> context.CreateQuery</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customers</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Select value C from Customers as C</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
    var res </span><span style="color: #000000;">=</span><span style="color: #000000;"> query.Execute(MergeOption.NoTracking) </span><span style="color: #0000FF;">as</span><span style="color: #000000;"> IEnumerable</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customers</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">;
    </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> res)
    {
        Console.WriteLine(c.CustomerID </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;"> City:</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> c.City);
    }
    Console.ReadKey();
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>working with EntitySql is simple because it resembles quite well the SQL syntax we are used to. Querying with <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/tag/linq/">LINQ</a> is simple too, but can surprise the novice user, suppose you write the following code</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:b32e207a-2019-46cb-bd55-85566acbaa0d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #008000;">//</span><span style="color: #008000;">Sample1();</span><span style="color: #008000;">
</span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
{
    var result </span><span style="color: #000000;">=</span><span style="color: #000000;"> from Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> context.Customers
                 select c;
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>the question is &quot;what is the query that gets executed to the database?&quot;. The answer is &quot;NONE&quot;. The reason is &quot;deferred execution&quot; of <a title="linq" href="http://www.codewrecks.com/blog/index.php/tag/linq/">linq</a> query, the above code does only define a query, it returns an object that represent the query, but actually it does not execute any SQL to the server. Now let&#8217;s look at this other piece of code</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:6f3241f8-3ac9-4e36-bb38-aab1a20b13ef" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #008000;">//</span><span style="color: #008000;">Sample1();</span><span style="color: #008000;">
</span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
{
    var query </span><span style="color: #000000;">=</span><span style="color: #000000;"> from Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> context.Customers
                select c;
    Console.WriteLine(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Count is {0}</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, query.Count());
    Console.WriteLine(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Count is {0}</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, query.Count());
    Console.WriteLine(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Count is {0}</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">, query.Count());
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This code actually executes three SQL query, since Count() is not a deferred operator, to know the result the query object needs to execute SQL against the datasource. This means that this code</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:50b9d6c4-f5db-4592-a046-2bc5f6521755" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #008080;">1</span> <span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
</span><span style="color: #008080;">2</span> <span style="color: #000000;">{
</span><span style="color: #008080;">3</span> <span style="color: #000000;">    var query </span><span style="color: #000000;">=</span><span style="color: #000000;"> from Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> context.Customers
</span><span style="color: #008080;">4</span> <span style="color: #000000;">                select c;
</span><span style="color: #008080;">5</span> <span style="color: #000000;">    query.GetEnumerator();
</span><span style="color: #008080;">6</span> <span style="color: #000000;">    query.GetEnumerator();
</span><span style="color: #008080;">7</span> <span style="color: #000000;">    query.GetEnumerator();
</span><span style="color: #008080;">8</span> <span style="color: #000000;">}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>execute three selects on the server. This behavior can surprise users that are not used to EF, someone believe that the real data is executed in line 3 and not at the time of the iteration, and they gets surprised when they see a SQL query issued to the database for each enumeration. The fact that the query is deferred can surprise but can be useful too. Let&#8217;s look at the following code.</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:1f1bfda1-fb39-4746-86ee-0a652e028a5d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
{
    var query </span><span style="color: #000000;">=</span><span style="color: #000000;"> from Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> context.Customers
                </span><span style="color: #0000FF;">where</span><span style="color: #000000;"> c.ContactName.Contains(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">d</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">)
                orderby c.City
                select c;
    </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> query.Where(cst </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> cst.CustomerID.StartsWith(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">a</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">)))
    {
        Console.WriteLine(c.CustomerID </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;"> City:</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> c.City);
    }
    </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> query.Where(cst </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> cst.CustomerID.StartsWith(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">r</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">)))
    {
        Console.WriteLine(c.CustomerID </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;"> City:</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> c.City);
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>since the query object build at the beginning is only a &quot;query object&quot; I can use to make subsequent queries based on the original value, in this example the query object create a constraint on the contact name and an order by City. From this base query I actually execute ii two time into two distinct enumeration, adding some more criteria each time, this is possible because all the Queryobject are composable because they support LINQ. Another important fact is that you cannot iterate the result of a query outside the scope of the context.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:5ed8db45-13ec-400b-8288-5c9655431b53" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;"><a title="IQueryable" href="http://www.codewrecks.com/blog/index.php/2008/03/04/iqueryable/">IQueryable</a></span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customers</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> query;
</span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
{
    query </span><span style="color: #000000;">=</span><span style="color: #000000;"> (from Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> context.Customers select c);
}
</span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> query)
{
    Console.WriteLine(c.CustomerID </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;"> City:</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> c.City);
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This code is wrong and it raise the exception &quot;The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.&quot; If the actual query gets executed at each iteration, when you iterate outside the context that creates the query, the underling SqlConnection is disposed and EF cannot issue query anymore. The obvious solution is to use a not deferred operator like ToList() to actually create a list of object that gets disconnected when the context is disposed, but they can be accessed without problem.</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ecb5c86d-d3d9-4361-999e-618a015daac6" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #008080;">1</span> <span style="color: #000000;">IList</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customers</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> query;
</span><span style="color: #008080;">2</span> <span style="color: #000000;"></span><span style="color: #0000FF;">using</span><span style="color: #000000;"> (NorthwindEntities context </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> NorthwindEntities())
</span><span style="color: #008080;">3</span> <span style="color: #000000;">{
</span><span style="color: #008080;">4</span> <span style="color: #000000;">    query </span><span style="color: #000000;">=</span><span style="color: #000000;"> (from Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> context.Customers select c).ToList();
</span><span style="color: #008080;">5</span> <span style="color: #000000;">}
</span><span style="color: #008080;">6</span> <span style="color: #000000;"></span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (Customers c </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> query)
</span><span style="color: #008080;">7</span> <span style="color: #000000;">{
</span><span style="color: #008080;">8</span> <span style="color: #000000;">    Console.WriteLine(c.CustomerID </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">&quot;</span><span style="color: #800000;"> City:</span><span style="color: #800000;">&quot;</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> c.City);
</span><span style="color: #008080;">9</span> <span style="color: #000000;">}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The only difference is that in line 4 I used ToList() to actually execute the query and store the result in a list in memory, so it can be accessed even when the context is disposed.</p>
<p>alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/EntityFramework" rel="tag">EntityFramework</a> <a target="_blank" href="http://technorati.com/tag/LINQ" rel="tag">LINQ</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2009/01/08/entity-framework-first-steps/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Extend BindingList with filter functionality</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/#comments</comments>
		<pubDate>Sat, 22 Nov 2008 11:14:16 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Languages]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Software Architecture]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/</guid>
		<description><![CDATA[If you still work with windows forms and cannot move all of your projects to WPF, you probably gets annoyed by the limitation of the standard BindingList&#60;T&#62; included in the framework. In an old post I showed how can you create a specialized BindingList&#60;T&#62; to support generic Find() thanks to reflection, but this is not [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F11%2F22%2Fextend-bindinglist-with-filter-functionality%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F11%2F22%2Fextend-bindinglist-with-filter-functionality%2F&amp;source=alkampfer&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you still work with windows forms and cannot move all of your projects to WPF, you probably gets annoyed by the limitation of the standard BindingList&lt;T&gt; included in the framework. In an <a href="http://www.codewrecks.com/blog/index.php/2007/10/12/bindinglistfind-and-notimplementedexception/">old post</a> I showed how can you create a specialized BindingList&lt;T&gt; to support generic Find() thanks to reflection, but this is not enough.</p>
<p>Another annoying limitation is that BindingList does not support IBindingListView. For those that does not knows this interface, it is the one used by the windows forms binding engine to support filtering and advanced sorting. I think that filtering is a real basic functionality needed by the binding engine, and in fact WPF support for filtering, sorting and grouping, make these operations really a breeze. But BindingList does not offers such a functionality.</p>
<p>My question is: <em>is it possible to inherits from BindingList&lt;T&gt; adding generic filtering support as for the DataView component</em>?. The answer is yes, here is a simple implementation.</p>
<p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:de608857-b994-4adb-b41b-595614a13852" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">string</span><span style="color: #000000;"> Filter
{
    </span><span style="color: #0000FF;">get</span><span style="color: #000000;">
    {
        </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> filterClause;
    }
    </span><span style="color: #0000FF;">set</span><span style="color: #000000;">
    {
        </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (filterClause </span><span style="color: #000000;">==</span><span style="color: #000000;"> value) </span><span style="color: #0000FF;">return</span><span style="color: #000000;">;
        filterClause </span><span style="color: #000000;">=</span><span style="color: #000000;"> value;
        DoFilter();
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>This is really simple, I implement the FIlter property with a simple check, if the filterClause changes, I call the DoFilter function.</p>
<p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:b8433304-b00b-4678-beb8-ee43366860bd" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF;">private</span><span style="color: #000000;"> Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> filterPredicate;

</span><span style="color: #0000FF;">private</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> original </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">();

</span><span style="color: #0000FF;">private</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> DoFilter()
{
    </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> (String.IsNullOrEmpty(filterClause))
    {
        Items.Clear();
        original.ForEach(e </span><span style="color: #000000;">=&gt;</span><span style="color: #000000;"> Items.Add(e));
    }
    </span><span style="color: #0000FF;">else</span><span style="color: #000000;">
    {
        original.Clear();
        original.AddRange(Items);
        Items.Clear();
        filterPredicate </span><span style="color: #000000;">=</span><span style="color: #000000;"> DynamicLinq.ParseToFunction</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">T, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(filterClause);
        </span><span style="color: #0000FF;">foreach</span><span style="color: #000000;"> (T element </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> original.Where(filterPredicate))
           Items.Add(element);
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>The implementation is based on this standard trick, if a filter is applied I copy all the elements in a temp list, then clear the base.Items list and add again only the elements that satisfies the filter. The big work is done in DynamicLinq.ParseToFunction that takes a string and dynamically compiles a function with that criteria. Here is a simple <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a> showing this class in action.</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3d8e51f2-bca9-4bd9-914c-4ded1c5abc29" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">[Test]
</span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> TestLogicAnd()
{
    Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> f </span><span style="color: #000000;">=</span><span style="color: #000000;"> DynamicLinq.ParseToFunction</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Name == 'Gian Maria' &amp;&amp; Age &gt; 5</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
    Assert.That(f(aCustomer), Is.True);
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
</p>
<p>Actually I use three main component, a tokenizer that takes a string and separates into token, then I pass the tokenized expression (is a List&lt;String&gt;) to another component that convert the infix form in postfix form (<a target="_blank" href="http://en.wikipedia.org/wiki/Reverse_Polish_notation">reverse Polish</a>), finally I create an expression from the reverse Polish form, creates a LambdaExpression on it and finally call compile() to make dynamic generation happens. The result is a dynamic generator of <a title="LINQ" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">LINQ</a> expression from a string. It still misses a lot, you cannot express condition on nested properties and parameter support is still experimental, but the basic is there.</p>
<p>The code is still under development, if you are curios you can check out the repository at <a target="_blank" title="http://dotnetmarcheproject.googlecode.com/svn/trunk" href="http://dotnetmarcheproject.googlecode.com/svn/trunk">http://dotnetmarcheproject.googlecode.com/svn/trunk</a> the code is still in rough form, but if you like it you can give it a shot. The class is the BindingListExt&lt;T&gt;</p>
<p>Alk.</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/BindingList" rel="tag">BindingList</a> <a target="_blank" href="http://technorati.com/tag/IBindingView" rel="tag">IBindingView</a> <a target="_blank" href="http://technorati.com/tag/Dynamic Expression Parser" rel="tag">Dynamic Expression Parser</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/11/22/extend-bindinglist-with-filter-functionality/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Dynamic Linq expression generator</title>
		<link>http://www.codewrecks.com/blog/index.php/2008/11/19/dynamic-linq-expression-generator/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2008/11/19/dynamic-linq-expression-generator/#comments</comments>
		<pubDate>Wed, 19 Nov 2008 09:32:17 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2008/11/19/dynamic-linq-expression-generator/</guid>
		<description><![CDATA[Some time ago I read this post that speaks about dynamic query library. I downloaded the code sample, it is interesting, but is bound to Linq to Sql, now I need an implementation of a Dynamic Linq Query generator. Since I already worked in the past with expression parser and I have some generic Infix [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F11%2F19%2Fdynamic-linq-expression-generator%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2008%2F11%2F19%2Fdynamic-linq-expression-generator%2F&amp;source=alkampfer&amp;style=normal&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Some time ago I read th<a target="_blank" href="http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx">is post</a> that speaks about dynamic query library. I downloaded the code sample, it is interesting, but is bound to <a title="Linq" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">Linq</a> to Sql, now I need an implementation of a Dynamic <a title="Linq" href="http://www.codewrecks.com/blog/index.php/category/net-framework/linq/">Linq</a> Query generator. Since I already worked in the past with expression parser and I have some generic Infix to postfix converter in a three hours of work I was able to put everything in a project and now I can write</p>
</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:86b428f9-16ad-4daf-a707-61093dc5eddd" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000;">Func</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;"> f </span><span style="color: #000000;">=</span><span style="color: #000000;"> DynamicLinq.ParseToFunction</span><span style="color: #000000;">&lt;</span><span style="color: #000000;">Customer, Boolean</span><span style="color: #000000;">&gt;</span><span style="color: #000000;">(</span><span style="color: #800000;">&quot;</span><span style="color: #800000;">Name == 'Gian Maria'</span><span style="color: #800000;">&quot;</span><span style="color: #000000;">);
Assert.That(f(aCustomer), Is.True);</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>This example shows that I can build a simple function that accepts an <a target="_blank" title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a> (Customer) and a string representing some conditions, then Iâ€™m able to dynamically parse the string, create an expression tree and call Compile on it to obtain a function that I can use in code. As soon as possible Iâ€™ll show a complete example if you are interested in.</p>
<p>alk.</p>
</p>
</p>
<p>Tags: <a target="_blank" href="http://technorati.com/tag/LINQ" rel="tag">LINQ</a> <a target="_blank" href="http://technorati.com/tag/Dynamic Expression Generation" rel="tag">Dynamic Expression Generation</a></p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2008/11/19/dynamic-linq-expression-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

