<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Benoît Laut - Linq</title>
    <link>http://benoitlaut.net/</link>
    <description>Let There Be Code</description>
    <language>en-us</language>
    <copyright>Benoît Laut</copyright>
    <lastBuildDate>Wed, 28 Oct 2009 11:18:07 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>benoit.laut@bewise.fr</managingEditor>
    <webMaster>benoit.laut@bewise.fr</webMaster>
    <item>
      <trackback:ping>http://benoitlaut.net/Trackback.aspx?guid=c8015c2f-1fbb-4952-8424-55d870140185</trackback:ping>
      <pingback:server>http://benoitlaut.net/pingback.aspx</pingback:server>
      <pingback:target>http://benoitlaut.net/PermaLink,guid,c8015c2f-1fbb-4952-8424-55d870140185.aspx</pingback:target>
      <dc:creator>Benoît Laut</dc:creator>
      <wfw:comment>http://benoitlaut.net/CommentView,guid,c8015c2f-1fbb-4952-8424-55d870140185.aspx</wfw:comment>
      <wfw:commentRss>http://benoitlaut.net/SyndicationService.asmx/GetEntryCommentsRss?guid=c8015c2f-1fbb-4952-8424-55d870140185</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Voici une petite méthode d’extension qui permet d’effectuer une recherche de manière
récursive dans un treeview en lui spécifiant un prédicat : 
</p>
        <p>
        </p>
        <div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:5e8d1b10-cdc7-493f-b0a7-7ca5da6ced5d" class="wlWriterEditableSmartContent">
          <pre name="code" class="c#">public static class TreeNodeExtension
{
	public static IEnumerable&lt;TreeNode&gt; FindNodes(this TreeNodeCollection nodesCollection, Func&lt;TreeNode, bool&gt; predicate)
	{
		var nodes = nodesCollection.Cast&lt;TreeNode&gt;();
		return nodes.SelectMany(n =&gt; FindNodes(n.Nodes, predicate).Union(nodes.Where(n2 =&gt; predicate(n2))));
	}
}
</pre>
        </div>
Le SelectMany permet de mettre à plat l’appel récursif. Si on utilise un Select à
la place du SelectMany on obtient une liste de liste de nœud. Le SelectMany est l’équivalent
de 2 from comme ceci : 
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:2e96c042-1deb-42bc-80a9-7f5d18bed2d0" class="wlWriterEditableSmartContent"><pre name="code" class="c#">return 	from n in nodes
		from child in n.Nodes
		where predicate(child)
		select child;

</pre></div>
Pour l’utiliser ce n’est pas bien compliqué… 
<p></p><p>
Je voudrai récupérer la liste des TreeNode de type FileNode et qui sont sélectionnés
: 
</p><div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c5635e59-97f6-4730-8f6c-e1e3a262db54" class="wlWriterEditableSmartContent"><pre name="code" class="c#">var checkedFilesNode = treeView1.Nodes.FindNodes(n =&gt; n is FileNode &amp;&amp; n.Checked == true);</pre></div><img width="0" height="0" src="http://benoitlaut.net/aggbug.ashx?id=c8015c2f-1fbb-4952-8424-55d870140185" /></body>
      <title>Méthode d’extension : Recherche récursive dans un Treeview</title>
      <guid isPermaLink="false">http://benoitlaut.net/PermaLink,guid,c8015c2f-1fbb-4952-8424-55d870140185.aspx</guid>
      <link>http://benoitlaut.net/2009/10/28/M%c3%a9thodeDextensionRechercheR%c3%a9cursiveDansUnTreeview.aspx</link>
      <pubDate>Wed, 28 Oct 2009 11:18:07 GMT</pubDate>
      <description>&lt;p&gt;
Voici une petite méthode d’extension qui permet d’effectuer une recherche de manière
récursive dans un treeview en lui spécifiant un prédicat : 
&lt;p&gt;
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:5e8d1b10-cdc7-493f-b0a7-7ca5da6ced5d" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;public static class TreeNodeExtension
{
	public static IEnumerable&amp;lt;TreeNode&amp;gt; FindNodes(this TreeNodeCollection nodesCollection, Func&amp;lt;TreeNode, bool&amp;gt; predicate)
	{
		var nodes = nodesCollection.Cast&amp;lt;TreeNode&amp;gt;();
		return nodes.SelectMany(n =&amp;gt; FindNodes(n.Nodes, predicate).Union(nodes.Where(n2 =&amp;gt; predicate(n2))));
	}
}
&lt;/pre&gt;
&lt;/div&gt;
Le SelectMany permet de mettre à plat l’appel récursif. Si on utilise un Select à
la place du SelectMany on obtient une liste de liste de nœud. Le SelectMany est l’équivalent
de 2 from comme ceci : 
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:2e96c042-1deb-42bc-80a9-7f5d18bed2d0" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;return 	from n in nodes
		from child in n.Nodes
		where predicate(child)
		select child;

&lt;/pre&gt;
&lt;/div&gt;
Pour l’utiliser ce n’est pas bien compliqué… 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
Je voudrai récupérer la liste des TreeNode de type FileNode et qui sont sélectionnés
: 
&lt;/p&gt;
&lt;div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:812469c5-0cb0-4c63-8c15-c81123a09de7:c5635e59-97f6-4730-8f6c-e1e3a262db54" class="wlWriterEditableSmartContent"&gt;&lt;pre name="code" class="c#"&gt;var checkedFilesNode = treeView1.Nodes.FindNodes(n =&amp;gt; n is FileNode &amp;amp;&amp;amp; n.Checked == true);&lt;/pre&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://benoitlaut.net/aggbug.ashx?id=c8015c2f-1fbb-4952-8424-55d870140185" /&gt;</description>
      <comments>http://benoitlaut.net/CommentView,guid,c8015c2f-1fbb-4952-8424-55d870140185.aspx</comments>
      <category>C#</category>
      <category>Linq</category>
    </item>
  </channel>
</rss>