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

<channel>
	<title>Ulrik B&#039;s Blog</title>
	<atom:link href="http://ulrikb.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ulrikb.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Thu, 20 May 2010 13:44:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ulrikb.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ulrik B&#039;s Blog</title>
		<link>http://ulrikb.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ulrikb.wordpress.com/osd.xml" title="Ulrik B&#039;s Blog" />
	<atom:link rel='hub' href='http://ulrikb.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Extension Method to Flatten Any Recursive Collection</title>
		<link>http://ulrikb.wordpress.com/2010/05/20/extension-method-to-flatten-any-recursive-collection/</link>
		<comments>http://ulrikb.wordpress.com/2010/05/20/extension-method-to-flatten-any-recursive-collection/#comments</comments>
		<pubDate>Thu, 20 May 2010 13:43:17 +0000</pubDate>
		<dc:creator>ulrikb</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ulrikb.wordpress.com/?p=22</guid>
		<description><![CDATA[Sometimes I wan&#8217;t to iterate all items in a recursive collection structure, e.g. the Controls hierarchy of Web Controls or Windows Forms Controls. Instead of coding a &#8220;flatten&#8221; method for each collection type, I&#8217;ve written a small extension method Flatten that allows you to easily recurse any structure: /// &#60;summary&#62; /// Flattens any recursive collection [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ulrikb.wordpress.com&amp;blog=12238210&amp;post=22&amp;subd=ulrikb&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes I wan&#8217;t to iterate all items in a recursive collection structure, e.g. the Controls hierarchy of Web Controls or Windows Forms Controls.</p>
<p>Instead of coding a &#8220;flatten&#8221; method for each collection type, I&#8217;ve written a small extension method Flatten that allows you to easily recurse any structure:</p>
<p><code></p>
<pre>
<div id="_mcePaste">/// &lt;summary&gt;
/// Flattens any recursive collection into a single list of all items.
/// &lt;/summary&gt;
/// &lt;typeparam name="T"&gt;Type of items in collection&lt;/typeparam&gt;
/// &lt;param name="collection"&gt;Root collection of T items&lt;/param&gt;
/// &lt;param name="recursor"&gt;Selector that returns the sub collection of a given item&lt;/param&gt;
/// &lt;param name="predicate"&gt;Predicate to apply to each item before returning it&lt;/param&gt;
/// &lt;returns&gt;List of all items&lt;/returns&gt;
public static IList&lt;T&gt; Flatten&lt;T&gt;(
  this IEnumerable&lt;T&gt; collection,
  Func&lt;T, IEnumerable&lt;T&gt;&gt; recursor,
  Func&lt;T, bool&gt; predicate)
{
  // Container for results
  var result = new System.Collections.Generic.List&lt;T&gt;();

  // Queue of yet unprocessed collections
  var queue = new Queue&lt;IEnumerable&lt;T&gt;&gt;();

  do
  {
    // Process current collection
    foreach (T control in collection)
    {
      queue.Enqueue(recursor(control));
      if (predicate == null || predicate(control))
        result.Add(control);
    }

    // Go to next or exit if done
    if (queue.Count &gt; 0)
      collection = queue.Dequeue();
    else
      return result;
  } while (true);
}</div>
</pre>
<p></code></p>
<p>The usage of it is simple:</p>
<p><code></p>
<pre>IList&lt;Control&gt; allControls = Controls.Cast&lt;Control&gt;().Flatten(c =&gt; c.Controls.Cast&lt;Control&gt;(), null);</pre>
<p></code><br />
<span style="font-family:Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;line-height:19px;white-space:normal;font-size:13px;">To avoid the ugly Cast&lt;Control&gt;() calls, you can define overloads of Flatten that works directly on ControlCollection. The reason why Cast is needed is that ControlCollection does not implement IEnumerable&lt;Control&gt;.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ulrikb.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ulrikb.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ulrikb.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ulrikb.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ulrikb.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ulrikb.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ulrikb.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ulrikb.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ulrikb.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ulrikb.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ulrikb.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ulrikb.wordpress.com/22/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ulrikb.wordpress.com/22/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ulrikb.wordpress.com/22/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ulrikb.wordpress.com&amp;blog=12238210&amp;post=22&amp;subd=ulrikb&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ulrikb.wordpress.com/2010/05/20/extension-method-to-flatten-any-recursive-collection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/87225ff0c7d3f247257f8a04bf5dc6d6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ulrikb</media:title>
		</media:content>
	</item>
		<item>
		<title>Executing a LINQ query asynchronously</title>
		<link>http://ulrikb.wordpress.com/2010/04/14/executing-a-linq-query-asynchronously/</link>
		<comments>http://ulrikb.wordpress.com/2010/04/14/executing-a-linq-query-asynchronously/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 08:27:13 +0000</pubDate>
		<dc:creator>ulrikb</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ulrikb.wordpress.com/?p=4</guid>
		<description><![CDATA[Sometimes I want to execute a costly LINQ query in parallel with other tasks or maybe in parallel with other costly queries. This can be useful, e.g. if you need to load a lot of lookup data not related to eachother. For that, I&#8217;ve made a small extension method: public static Func&#60;T[]&#62; Async&#60;T&#62;(this IEnumerable&#60;T&#62; enumerable) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ulrikb.wordpress.com&amp;blog=12238210&amp;post=4&amp;subd=ulrikb&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sometimes I want to execute a costly LINQ query in parallel with other tasks or maybe in parallel with other costly queries. This can be useful, e.g. if you need to load a lot of lookup data not related to eachother.</p>
<p>For that, I&#8217;ve made a small extension method:</p>
<p><code>
<pre>public static Func&lt;T[]&gt; Async&lt;T&gt;(this IEnumerable&lt;T&gt; enumerable)
{
  System.Diagnostics.Debug.Assert(!(enumerable is ICollection),
  "Async does not work on arrays/lists/collections, only on true enumerables/queryables.");

  // Create delegate to exec async
  Func&lt;IEnumerable&lt;T&gt;, T[]&gt; work = e =&gt; e.ToArray();

  // Launch it
  IAsyncResult r = work.BeginInvoke(enumerable, null, null);

  // Return method that will block until completed and rethrow exceptions if any
  return () =&gt; work.EndInvoke(r);
}</pre>
<p></code>
<p style="text-align:left;"><span style="font-family:Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;">The usage is then very simple as illustrated by the following code snippet:</span></p>
<p><code>
<pre>// Define some expensive query
IQueryable&lt;string&gt; myExpensiveQuery = context.SystemLog.Where(l =&gt; l.Timestamp &gt;= DateTime.Today.AddDays(-10));

// Start async processing
Func&lt;string[]&gt; waitForQueryData = myExpensiveQuery.Async();

// Do a lot of other work, e.g. other queries

// Need my query result now, so block until it's ready and get result
string[] myQueryResults = waitForQueryData();
</pre>
<p></code></p>
<p>If the query throws an exception you will get it in the waitForQueryData() call.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ulrikb.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ulrikb.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ulrikb.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ulrikb.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ulrikb.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ulrikb.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ulrikb.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ulrikb.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ulrikb.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ulrikb.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ulrikb.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ulrikb.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ulrikb.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ulrikb.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ulrikb.wordpress.com&amp;blog=12238210&amp;post=4&amp;subd=ulrikb&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ulrikb.wordpress.com/2010/04/14/executing-a-linq-query-asynchronously/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/87225ff0c7d3f247257f8a04bf5dc6d6?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ulrikb</media:title>
		</media:content>
	</item>
	</channel>
</rss>
