<?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>dasz.at - Benutzbare Technologie &#187; Linq</title>
	<atom:link href="http://dasz.at/index.php/category/microsoft/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://dasz.at</link>
	<description>Benutzbare Technologie</description>
	<lastBuildDate>Fri, 30 Jul 2010 17:07:19 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>.Net microbenchmarking</title>
		<link>http://dasz.at/index.php/2009/01/net-microbenchmarking/</link>
		<comments>http://dasz.at/index.php/2009/01/net-microbenchmarking/#comments</comments>
		<pubDate>Sat, 31 Jan 2009 17:05:41 +0000</pubDate>
		<dc:creator>David Schmitt</dc:creator>
				<category><![CDATA[Benchmarking]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[StackOverflow]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://dasz.at/index.php/2009/01/net-microbenchmarking/</guid>
		<description><![CDATA[In a recent discussion on StackOverflow about performance of arrays vs. lists and for vs. foreach, Jon Skeet created a little microbenchmarking framework and posted his detailed findings on his blog.
First, my local results of the benchmark. I compiled them as Release under VS 2008 SP1. Running without debugging on a Q6600@2.40GHz, .NET 3.5 SP1. [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent discussion on <a href="http://stackoverflow.com/">StackOverflow</a> about performance of <a href="http://stackoverflow.com/questions/454916/performance-of-arrays-vs-lists">arrays vs. lists</a> and <a href="http://stackoverflow.com/questions/472191/c-for-vs-foreach">for vs. foreach</a>, <a href="http://stackoverflow.com/users/22656/jon-skeet">Jon Skeet</a> created a little microbenchmarking framework and posted <a href="http://msmvps.com/blogs/jon_skeet/archive/2009/01/29/for-vs-foreach-on-arrays-and-lists.aspx">his detailed findings on his blog</a>.</p>
<p>First, my local results of the benchmark. I compiled them as Release under VS 2008 SP1. Running without debugging on a Q6600@2.40GHz, .NET 3.5 SP1. They pretty exactly match Jon&#8217;s numbers so I won&#8217;t duplicate them here, but you can find them in the page&#8217;s source.</p>
<p><!--><br />
============ Doubles ============<br />
============ double[] ============<br />
For                1,00<br />
ForHoistLength     1,00<br />
ForEach            1,00<br />
IEnumerableForEach 8,28<br />
Enumerable.Sum     8,27</p>
<p>============ List<double> ============<br />
For                 2,00<br />
ForHoistLength      1,43<br />
ForEach             6,02<br />
IEnumerableForEach 14,03<br />
Enumerable.Sum     14,04</p>
<p>============ Ints ============<br />
============ int[] ============<br />
For                 1,00<br />
ForHoistLength      2,06<br />
ForEach             1,38<br />
IEnumerableForEach 15,46<br />
Enumerable.Sum     16,06</p>
<p>============ List<int> ============<br />
For                 2,84<br />
ForHoistLength      3,53<br />
ForEach             4,86<br />
IEnumerableForEach 26,33<br />
Enumerable.Sum     26,33</p>
<p><!--></p>
<p>Out of interest how the differences are when there is more to do in the loop&#8217;s body, I added a test suite that formatted the sum as string and appended it to a <code>StringBuilder</code>:</p>
<p><span id="more-74"></span></p>
<pre class="brush: c#; ">

var sbArraySuite = TestSuite.Create(&quot;build Strings from Array&quot;, doubleArray, String.Join(&quot;&quot;, doubleArray.Select(d =&gt; String.Format(&quot;{0:0.00}\n&quot;, d)).ToArray()))
    .Add(input =&gt;
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i &lt; input.Length; i++)
            sb.AppendFormat(&quot;{0:0.00}\n&quot;, input[i]);
        return sb.ToString();
    }, &quot;For&quot;)
    .Add(input =&gt;
    {
        StringBuilder sb = new StringBuilder(); int length = input.Length;
        for (int i = 0; i &lt; length; i++)
            sb.AppendFormat(&quot;{0:0.00}\n&quot;, input[i]);
        return sb.ToString();
    }, &quot;ForHoistLength&quot;)
    .Add(input =&gt;
    {
        StringBuilder sb = new StringBuilder();
        foreach (double d in input)
            sb.AppendFormat(&quot;{0:0.00}\n&quot;, d);
        return sb.ToString();
    }, &quot;ForEach&quot;)
    .Add(IEnumerableForEachToString)
    .RunTests();

    ...

    static string IEnumerableForEachToString(IEnumerable&lt;double&gt; input)
    {
        StringBuilder sb = new StringBuilder();
        foreach (double d in input)
        {
            sb.AppendFormat(&quot;{0:0.00}\n&quot;, d);
        }
        return sb.ToString();
    }
</pre>
<p>Which gives these results:</p>
<pre>
============ build Strings from Array ============
For                        1,01
ForHoistLength             1,01
ForEach                    1,01
IEnumerableForEachToString 1,03

============ build Strings from List ============
For                        1,00
ForHoistLength             1,00
ForEach                    1,02
IEnumerableForEachToString 1,05
</pre>
<p>Suddenly the relative differences between for/foreach and array/list collapse to 1-5%. Which confirms my intuition that algorithms usually matter much more and such optimisations are quite useless. At least we all had good fun on a saturday <img src='http://dasz.at/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://dasz.at/index.php/2009/01/net-microbenchmarking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic search with Linq</title>
		<link>http://dasz.at/index.php/2008/12/dynamic-search-with-linq/</link>
		<comments>http://dasz.at/index.php/2008/12/dynamic-search-with-linq/#comments</comments>
		<pubDate>Wed, 31 Dec 2008 16:50:06 +0000</pubDate>
		<dc:creator>Arthur Zaczek</dc:creator>
				<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://dasz.at/index.php/2008/12/dynamic-search-with-linq/</guid>
		<description><![CDATA[The Problem: Find all sentences with either &#8220;fox&#8221; and &#8220;dog&#8221; and &#8220;fox&#8221; or &#8220;cat&#8221;.


string[] sentences = new string[] {
  &#34;The quick brown fox jumps over the lazy dog&#34;,
  &#34;The quick brown fox jumps over the lazy cat&#34;};
string[] findAnd = new string[] { &#34;fox&#34;, &#34;dog&#34; };
string[] findOr = new string[] { &#34;dog&#34;, &#34;cat&#34; };

The [...]]]></description>
			<content:encoded><![CDATA[<p>The Problem: Find all sentences with either &#8220;fox&#8221; and &#8220;dog&#8221; and &#8220;fox&#8221; or &#8220;cat&#8221;.</p>
<pre class="brush: c#; ">

string[] sentences = new string[] {
  &quot;The quick brown fox jumps over the lazy dog&quot;,
  &quot;The quick brown fox jumps over the lazy cat&quot;};
string[] findAnd = new string[] { &quot;fox&quot;, &quot;dog&quot; };
string[] findOr = new string[] { &quot;dog&quot;, &quot;cat&quot; };
</pre>
<p>The first part (find all) is simple:</p>
<pre class="brush: c#; ">

var queryand = sentences.AsQueryable();
foreach (string find in findAnd)
{
  string f = find;
  queryand = queryand.Where(s =&gt; s.Contains(find));
}

Console.WriteLine(string.Join(&quot;\n&quot;, queryand.ToArray()));
</pre>
<p>But how to implement to Or part? Well, google is my best friend. I found the following solution:</p>
<p><a href="http://www.albahari.com/nutshell/predicatebuilder.aspx">Predicate Builder on C# 3.5 in a Nutshell</a></p>
<p>But this solution does not work with Entity Framework: <a href="http://stackoverflow.com/questions/110314/linq-to-entities-building-where-clauses-to-test-collections-within-a-many-to-ma#131551">Predicate Builder and Entity Framework on stackoverflow.com</a></p>
<p>After implementing this solution your code will look like this:</p>
<pre class="brush: c#; ">

var predicate = Extensions.False&lt;string&gt;();
foreach (string find in findOr)
{
  string f = find;
  predicate = predicate.Or(s =&gt; s.Contains(f));
}
var queryor = sentences.AsQueryable().Where(predicate);
Console.WriteLine(string.Join(&quot;\n&quot;, queryor.ToArray()));
</pre>
<p>P.S.: You can find an ExpressionVisitor here: <a href="http://msdn.microsoft.com/en-us/library/bb882521.aspx">ExpressionVisitor at MSDN</a></p>
]]></content:encoded>
			<wfw:commentRss>http://dasz.at/index.php/2008/12/dynamic-search-with-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
