<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Derik Whittaker : Linq, Development</title><link>http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/Development/default.aspx</link><description>Tags: Linq, Development</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Linq rocks, except when it doesn’t.  Can anyone spot the bug in this code?</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2011/01/21/linq-rocks-except-when-it-doesn-t-can-anyone-spot-the-bug-in-this-code.aspx</link><pubDate>Fri, 21 Jan 2011 23:10:59 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:64752</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=64752</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=64752</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2011/01/21/linq-rocks-except-when-it-doesn-t-can-anyone-spot-the-bug-in-this-code.aspx#comments</comments><description>&lt;p&gt;I am in the middle of building out some wcf endpoints to support the Dimecasts.net WP7 application I am going to publish.&amp;#160; The code below is some of my main logic which helps grab the episodes.&amp;#160; Because the data for Dimecasts is not very volatile I am going to do some basic (aka primitive) caching.&amp;#160; &lt;/p&gt;  &lt;p&gt;What I am looking to do is save the amount of data which needs to be gathered from the db.&amp;#160; The intent of the code below is to get all data from cache which is greater then the last known episode which is cached on the phone.&amp;#160; If data is found in the cache we will adjust the ‘last known episode’ in order to again reduce the db hit.&amp;#160; However, there is a slight bug.&lt;/p&gt;  &lt;p&gt;Who can spot the bug?&lt;/p&gt;  &lt;pre class="c-sharp" name="code"&gt;        public IList&amp;lt;EpisodeModel&amp;gt; FetchEpisodes( Int32 lastKnownEpisodeId )
        {
            var episodes = new List();
            var cachedEpisodes = Cache.FetchEpisodes()
                .Where( x =&amp;gt; x.EpisodeNumber &amp;gt; lastKnownEpisodeId )
                .OrderBy( x =&amp;gt; x.Id );

            if ( cachedEpisodes.Count() != 0 )
            {
                // reset the last known id in order to reset the reset
                lastKnownEpisodeId = cachedEpisodes.LastOrDefault().Id;

                episodes.AddRange( cachedEpisodes );
            }

            // more code which does not matter here.....

            return episodes;
        }&lt;/pre&gt;

&lt;p&gt;If you know the issue post it in the comments.&amp;#160; If it is not ‘solved’ I will post an update with the solution and the reason.&lt;/p&gt;

&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=64752" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category></item><item><title>Sorting List's the .Net 3.5 Way</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/28/sorting-list-s-the-net-3-5-way.aspx</link><pubDate>Sat, 29 Mar 2008 02:09:13 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39854</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=39854</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=39854</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/28/sorting-list-s-the-net-3-5-way.aspx#comments</comments><description>&lt;p&gt;This post is another in the recent string of How-to&amp;#39;s.&amp;nbsp; But it is just so hard to resist.&amp;nbsp; .Net 3.5 has so many cool features, it is just fun to explore them all.&lt;/p&gt; &lt;p&gt;Ok, so here is the issue we are going to solve today.&amp;nbsp; We have a list of entities and we need to sort them.&amp;nbsp; In this post I will show you 3 different ways to accomplish this task, all producing the same result, but using different techniques (hey &lt;a href="http://msmvps.com/blogs/peterritchie/default.aspx" target="_blank"&gt;Peter&lt;/a&gt;, go ahead and show me a 4th and better way :) ).&lt;/p&gt; &lt;p&gt;A little up front declarations.&amp;nbsp; Each method will use the same list, so to save space I will declare this now.&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;List userList = new List
    {
        new User {FirstName = &amp;quot;Andy&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Opie&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Bee&amp;quot;, LastName = &amp;quot;Tayler&amp;quot;},
        new User {FirstName = &amp;quot;Barney&amp;quot;, LastName = &amp;quot;Fife&amp;quot;},
        new User {FirstName = &amp;quot;Goomer&amp;quot;, LastName = &amp;quot;Pile&amp;quot;}
};
&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 1 -- Using the Sort with IComarer&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;public void Sort_OldSchool()
{
    userList.Sort(new UserCompare());
}

// Sort class
private class UserCompare : IComparer&amp;lt;User&amp;gt;
 {
    public int Compare( User x, User y )
    {
    return x.LastName.CompareTo( y.LastName );
}
&lt;/pre&gt;
&lt;p&gt;The above works, but requires you to create a custom class that implements the IComparer&amp;lt;User&amp;gt; interface to do the trick.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 2 -- Using Lambda and extension methods&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;[Test]
public void Sort_UsingLambda()
{
	userList = userList.OrderBy( user =&amp;gt; user.LastName ).ToList();
}
&lt;/pre&gt;
&lt;p&gt;This way works great, it is clean and very readable.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Sort Method 3 -- Use Linq2Objects&lt;/strong&gt;&lt;/p&gt;&lt;pre class="c-sharp" name="code"&gt;[Test]
public void Sort_UsingLinq()
{
	userList = ( from u in userList
        	orderby u.LastName, u.FirstName
                select u ).ToList();
}
&lt;/pre&gt;
&lt;p&gt;By using Linq you open yourself up to a whole new level of flexibility. You can not only sort (OrderBy), you can filter, return only a select number of rows, etc, etc.&amp;nbsp; Basically you can do anything that Linq provides.&lt;/p&gt;
&lt;p&gt;As you can see from the above, they all accomplish the same goal but method 2 &amp;amp; 3 are just more fun.&amp;nbsp; Besides, if you are going to be using .Net 3.5 you might as well use the new features.&lt;/p&gt;
&lt;p&gt;BTW, all the methods (except 3 since it sorts by LastName and FirstName) above produce the following output.&lt;br /&gt;&lt;em&gt;Barney Fife&lt;br /&gt;Goomer Pile&lt;br /&gt;Andy Tayler&lt;br /&gt;Opie Tayler&lt;br /&gt;Bee Tayler&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Till next time,&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=39854" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Development/default.aspx">Development</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/.Net/default.aspx">.Net</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Opinion/default.aspx">Opinion</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/HowTo/default.aspx">HowTo</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Linq/default.aspx">Linq</category><category domain="http://devlicio.us/blogs/derik_whittaker/archive/tags/Lambda/default.aspx">Lambda</category></item></channel></rss>