<?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 : Opinion, Linq</title><link>http://devlicio.us/blogs/derik_whittaker/archive/tags/Opinion/Linq/default.aspx</link><description>Tags: Opinion, Linq</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><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><item><title>Linq2Sql, my thoughts after a few hours of learning</title><link>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/13/linq2sql-my-thoughts-after-a-few-hours-of-learning.aspx</link><pubDate>Thu, 13 Mar 2008 18:59:48 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:39658</guid><dc:creator>Derik Whittaker</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/rsscomments.aspx?PostID=39658</wfw:commentRss><wfw:comment xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://devlicio.us/blogs/derik_whittaker/commentapi.aspx?PostID=39658</wfw:comment><comments>http://devlicio.us/blogs/derik_whittaker/archive/2008/03/13/linq2sql-my-thoughts-after-a-few-hours-of-learning.aspx#comments</comments><description>&lt;p&gt;Today I started to play/learn the basics on Linq2SQL and I have to say, it is pretty cool.&amp;nbsp; However, within the first 20 minutes I came to a cross roads, I was stuck.&amp;nbsp; I am torn between the 2 different ways to connect to, and query from, a data source.&amp;nbsp; I don&amp;#39;t know which I like better.&amp;nbsp; &lt;/p&gt; &lt;p&gt;Below are the 2 different ways to connect, as well as my opinions as to the pros/cons on each one.&amp;nbsp; Please take into account I have NO real world experience using Linq, these are just my thoughts on the subject&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Approach 1&lt;br /&gt;&lt;/strong&gt;The most common way I have seen to use Linq2SQL is to generate a custom DataContext class (.dbml class) via the IDE.&amp;nbsp; This will generate all your DTO classes for you as well as the mappings between tables/objects.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;Pros&lt;/li&gt; &lt;ul&gt; &lt;li&gt;Creates the DTO classes that map directly to your database tables for you.&amp;nbsp; This can save you a bit of time versus creating the DTO&amp;#39;s buy hand.&lt;/li&gt; &lt;li&gt;When the DB schema changes, you simply have to refresh the IDE and all the changes are brought down&lt;/li&gt; &lt;li&gt;No need to sprinkle your DTO&amp;#39;s with attributes to allows Linq to use them. (ie [Table(Name=&amp;quot;Sport&amp;quot;)]&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Cons&lt;/li&gt; &lt;ul&gt; &lt;li&gt;The generated code creates a lot of overhead.&amp;nbsp; For example, the default set code will raise 4 events as well as set the new value.&amp;nbsp; If you are using these events, great, but if not you have wasted cycles.&lt;/li&gt; &lt;li&gt;Gives you less control over your DTO objects&lt;/li&gt; &lt;li&gt;Would NOT work well if you are trying to back in Linq2Sql into an existing application&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Approach 2&lt;/strong&gt;&lt;br /&gt;The second way is to create a standard DataContext and create the mappings on the fly.&amp;nbsp; This way does NOT generate your DTO&amp;#39;s you &lt;/p&gt; &lt;ul&gt; &lt;li&gt;Pros&lt;/li&gt; &lt;ul&gt; &lt;li&gt;You can use your existing DTO&amp;#39;s (assuming you have some)&lt;/li&gt; &lt;li&gt;You have 100% control over how your DTO&amp;#39;s are created/generated&lt;/li&gt; &lt;li&gt;No events are fired when you set properties, unlike the generated DTO&amp;#39;s.&amp;nbsp; This is assuming you don&amp;#39;t WANT the events. &lt;/li&gt; &lt;li&gt;Would work well in the scenario where you are trying to back Linq2Sql into an existing application.&lt;/li&gt; &lt;li&gt;Because you have to hand wire the DTO&amp;#39;s to the db tables, you become more aware of what is going one and are more able to fix &amp;#39;odd&amp;#39; issues as the appear.&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;Cons&lt;/li&gt; &lt;ul&gt; &lt;li&gt;You need to sprinkle your existing DTO&amp;#39;s with various attributes to all them to be mapped to you database tables.&lt;/li&gt; &lt;li&gt;When your DB schema changes, you need to update your DTO&amp;#39;s by hand to reflect the changes.&lt;/li&gt; &lt;li&gt;You need to use the db.GetTable&amp;lt;EntityHere&amp;gt;() (db here is a DataContext instance) in order for the context to know about a given table.&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt; &lt;p&gt;Ok so there you have it, both ways to connect to the db using Linq along with my thoughts on pros/cons.&amp;nbsp; At the end of the day, I think I like the idea of &lt;strong&gt;Approach 2 &lt;/strong&gt;because I like to have more control over what my code does, although I have to admit I like the auto generation capabilities of&lt;strong&gt; Approach 1&lt;/strong&gt;.&amp;nbsp; I wonder if my attitude with respect to which approach to use will change over time, who knows:)&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=39658" width="1" height="1"&gt;</description><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/Linq/default.aspx">Linq</category></item></channel></rss>