<?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>Tyler Butler &#187; code</title>
	<atom:link href="http://www.tylerbutler.com/tags/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tylerbutler.com</link>
	<description></description>
	<lastBuildDate>Fri, 14 Aug 2009 22:09:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>LINQ Confusion</title>
		<link>http://www.tylerbutler.com/2009/03/linq-confusion/</link>
		<comments>http://www.tylerbutler.com/2009/03/linq-confusion/#comments</comments>
		<pubDate>Sun, 15 Mar 2009 07:47:38 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[agricola]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[code]]></category>

		<guid isPermaLink="false">http://www.tylerbutler.com/?p=341</guid>
		<description><![CDATA[I find myself using LINQ a lot in my C# code these days. I use collections all over the place, and there&#8217;s no doubt that LINQ makes sorting and slicing collections a lot simpler code-wise.
In my most recent weekend project, I need to randomly sort a list of cards, which are represented by an Action [...]]]></description>
			<content:encoded><![CDATA[<p>I find myself using LINQ a lot in my C# code these days. I use collections all over the place, and there&#8217;s no doubt that LINQ makes sorting and slicing collections a lot simpler code-wise.</p>
<p>In my most recent weekend project, I need to randomly sort a list of cards, which are represented by an <strong>Action</strong> class. After some quick searching, I found <a href="http://www.dailycoding.com/Posts/random_sort_a_list_using_linq.aspx">some articles</a> that indicated the best way to do this would be to sort the list by random GUID. This makes sense, though I certainly wouldn’t have thought of it on my own.</p>
<p>The examples given all worked, but not with my lists… With the following code, the compiler spits out several errors:</p>
<pre class="brush: c#;">List&lt;Action&gt; cards = new List&lt;Action&gt;();
cards.Add( new OneCattle() );
cards.Sort( a =&gt; Guid.NewGuid() ).ToList&lt;Action&gt;();

Error    1    Delegate 'System.Comparison&lt;agricola.Action&gt;' does not take '1' arguments
Error    2    Cannot convert lambda expression to type 'System.Collections.Generic.IComparer&lt;agricola.Action&gt;' because it is not a delegate type
Error    3    Cannot implicitly convert type 'System.Guid' to 'int'
Error    4    Cannot convert lambda expression to delegate type 'System.Comparison&lt;agricola.Action&gt;' because some of the return types in the block are not implicitly convertible to the delegate return type </pre>
<p>However, using a more explicit LINQ query without a lambda expression seems to work fine:</p>
<pre class="brush: c#;">var q = from a in cards
        orderby Guid.NewGuid()
        select a;
List&lt;Action&gt; r = q.ToList&lt;Action&gt;();</pre>
<p>Anybody know why this is? I haven’t wrapped my head around lambda expressions and the theory behind LINQ to understand what the root cause is…</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerbutler.com/2009/03/linq-confusion/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
