<?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>Tech News, Tech Resources, Technology Articles, Gadget News, Computer News &#187; LINQ</title>
	<atom:link href="http://www.comteken.com/tag/linq/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.comteken.com</link>
	<description>IT news, software technology, IT resources, computer software, laptops, desktops,  information systems,  hardware technology, multimedia, Windows OS, linux clients, network solution, easy internet, essential guides, domains, webhosting, web program, database programming</description>
	<lastBuildDate>Fri, 27 Jan 2012 11:22:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Improving Performance With LINQ</title>
		<link>http://www.comteken.com/database-programming/improving-performance-with-linq/</link>
		<comments>http://www.comteken.com/database-programming/improving-performance-with-linq/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 15:58:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Database Programming]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=30</guid>
		<description><![CDATA[<a href="http://www.comteken.com/database-programming/improving-performance-with-linq/"><img align="left" hspace="5" width="100" height="100" src="http://www.comteken.com/wp-content/plugins/thumbnail-for-excerpts/tfe_no_thumb.png" class="alignleft wp-post-image tfe" alt="" title="" /></a>While the title of this article may seem misleading, you actually can achieve incredible performance gains by updating your web application to use LINQ. This article will dispell some common myths about LINQ and demonstrate how LINQ can practically increase performance. The examples in this article will be using LINQ to SQL, but the principles [...]]]></description>
			<content:encoded><![CDATA[<p>While the title of this article may seem misleading, you actually can achieve incredible performance gains by updating your web application to use LINQ. This article will dispell some common myths about LINQ and demonstrate how LINQ can practically increase performance. The examples in this article will be using LINQ to SQL, but the principles apply to LINQ to Entities as well.</p>
<p>First of all, it&#8217;s important to realize what LINQ is and what it isn&#8217;t. LINQ is a language feature you&#8217;ll see in Visual Studio 2008 (Visual Studio Orcas) and beyond. It&#8217;s not dependant on any particular .NET language as you can use it in C# or VB.NET. LINQ stands for &#8220;Language Integrated Query&#8221;, but its usage goes beyond language capabilities alone.</p>
<p>It is true that the language speeds up development by allowing you to use a simple query-syntax in C# or VB.NET to query data from a HashTable, XML file or SQL Database. And it&#8217;s also true that you can have Visual Studio 2008 write tons of code for you to build classes for your data tables (or even to build SQL tables from your classes). However, this article will demonstrate runtime performance gains you&#8217;ll see by using some built in features that come with the language as well.</p>
<p>The benefits we&#8217;ll discuss are:</p>
<ul>
<li>Intelligent query translations (such as faster data pagination).</li>
<li>Redundant query checking to prevent needless data hits.</li>
<li>Redundant update checking to ensure minimal data transfers.</li>
</ul>
<h3>Intelligent Query Translations</h3>
<p>One of the built-in benefits of using LINQ is that methods that are deemed &#8220;best practice&#8221; are used in the underlying technologies. For instance, if you were to use a standard ASP.NET 2.0 SqlDataSource, and you bound that data to a GridView with pagination turned on, you might not realize that you&#8217;re transferring your entire data table just to use a few rows at a time in your paginated GridView. This results in some heavy queries, heavy data transfers and often times slow page loads.</p>
<p>The best place to do pagination would be on the SQL server itself. That way you are only transferring the few rows that are desired, saving you much network bandwidth and time. Ultimately, LINQ isn&#8217;t doing anything that you couldn&#8217;t do yourself, but most developers do not know how to do <a rel="nofollow" href="http://www.singingeels.com/Articles/Pagination_In_SQL_Server_2005.aspx">Pagination In SQL Server 2005</a>, or they may not have time to change all of their queries to do so.</p>
<p>Again, if you take that simple &#8220;SqlDataSource&#8221; example above, but this time bind two ASP.NET controls to it (let&#8217;s say a GridView and a DropDownList), then you&#8217;ll be hitting your SQL server twice, and download all that data twice. This brings us to the next performance gain in LINQ.</p>
<h3>Redundant Query Checking</h3>
<p>Some of the biggest performance gains that I&#8217;ve benefited from by switching to LINQ is it&#8217;s ability to look at the &#8220;context&#8221; of your request to determine if it needs to hit the database again. If you&#8217;ve already started to use LINQ yourself, but you don&#8217;t know what I&#8217;m talking about, then you&#8217;ll probably have a new appreciation for LINQ in a few minutes.</p>
<p>LINQ uses a &#8220;DataContext&#8221; object to determine what connection string to use, what objects to map to and other internal things. But you should be careful not to think of the LINQ data context class as you would think of a traditional &#8220;SqlConnection&#8221;. If you are transitioning from using SqlConnections and SqlCommands you&#8217;ll probably code something like this:</p>
<div><span>// Example of doing your own straight SQL query:</span><br />
<span>public</span> <span>static</span> IEnumerable GetMyData()<br />
{<br />
<span>using</span> (SqlConnection myConnection = <span>new</span> SqlConnection(myConnectionString))<br />
{<br />
<span>using</span> (SqlCommand myCommand = myConnection.CreateCommand())<br />
{<br />
myCommand.CommandText = <span>&#8220;SELECT * FROM dbo.myTable&#8221;</span>;</p>
<p><span>using</span> (SqlDataAdapter myAdapter = <span>new</span> SqlDataAdapter(myCommand))<br />
{<br />
DataTable resultTable = <span>new</span> DataTable();</p>
<p>myAdapter.Fill(resultTable);</p>
<p><span>return</span> resultTable.Rows;<br />
}<br />
}<br />
}<br />
}</div>
<p>If you translated the above method into LINQ, then your method might look something like this:</p>
<div><span>// Example of <span>using</span> LINQ to query the <span>&#8220;Clients&#8221;</span> table:</span><br />
<span>public</span> <span>static</span> IEnumerable GetMyData()<br />
{<br />
<span>using</span> (MyDataContext myContext = <span>new</span> MyDataContext())<br />
{<br />
<span>return</span> <span>from</span> myRow <span>in</span> myContext.Clients <span>select</span> myRow;<br />
}<br />
}</div>
<p>This at first seems like an acceptable conversion from using a SqlConnection to using LINQ, but you&#8217;re missing out on so much (which you&#8217;ll see in a minute).</p>
<p>This is pretty much a 1 to 1 switch from writing your own SQL query to writing a query in LINQ. It is noteworthy that it takes a lot less code to query a database. However, these two code blocks will do the same thing, and you won&#8217;t gain any performance from calling the LINQ method instead of calling the straight SQL method. Even if you call both of these functions twice in a row, you won&#8217;t gain any performance because we are not utilizing the LINQ context correctly.</p>
<p>If we extend the range of our context to last for the entire web request (not just for the life of the function call) then we will begin to see performance increases. Before we examine how to do that, let&#8217;s look at this next example that should help explain how the context works. We&#8217;ll change our above method, &#8220;GetMyData&#8221;, so that we can pass in our own context. Here&#8217;s the code:</p>
<div><span>// Change the GetMyData method to take <span>in</span> an existing context.</span><br />
<span>public</span> <span>static</span> IEnumerable GetMyData(MyDataContext myContext)<br />
{<br />
<span>return</span> <span>from</span> myRow <span>in</span> myContext.Clients <span>select</span> myRow;<br />
}<span></p>
<p>// This will demonstrate how we are going to gain performance with LINQ:</span><br />
<span>protected</span> <span>void</span> Page_Load(<span>object</span> sender, EventArgs e)<br />
{<span><br />
// First, we create our data context just like we did before.</span><br />
MyDataContext myContext = <span>new</span> MyDataContext();<span></p>
<p>// Calling <span>this</span> function the first time will hit SQL and query the data.</span><br />
GetMyData(myContext);<span></p>
<p>// LINQ <span>is</span> smart enough to realize that we&#8217;ve already used <span>this</span> query</span><span><br />
// against <span>this</span> data context, so it won&#8217;t hit SQL again. Instead it will</span><span><br />
// simply <span>return</span> the same result <span>set</span> <span>as</span> last time.</span><br />
GetMyData(myContext);<span></p>
<p>// This <span>is</span> a bit ridiculous, but you <span>get</span> the idea.</span><br />
<span>for</span> (<span>int</span> i = 0; i &lt; 100; i++)<br />
{<br />
GetMyData(myContext);<br />
}<br />
}</div>
<p>The way to achieve the performance benefits of using the same data context during the entire page request is to store your data context instance in the HttpContext Items collection (which is alive for the duration of the thread). We&#8217;ll make a static property that will give us a data context instance, but because we are using the current HttpContext, we won&#8217;t have cross thread issues. You can read a little more about the HttpContext in this article: <a rel="nofollow" href="http://www.singingeels.com/Articles/Connect_Your_Site_With_HttpContext_Items.aspx">Connect Your Site With HttpContext Items</a>. Let&#8217;s look at the code to see how to achieve this:</p>
<div><span>public</span> <span>static</span> <span>class</span> DataAccessLayer<br />
{<span><br />
// This <span>object</span> <span>is</span> a <span>&#8220;key&#8221;</span> to the HttpContext Items dictionary.</span><br />
<span>private</span> <span>static</span> <span>object</span> _eelsContextKey = <span>new</span> <span>object</span>();<span></p>
<p>// This will always <span>return</span> an instance of the MyDataContext</span><span><br />
// (which was created by a LINQ wizard <span>in</span> Visual Studio 2008). It</span><span><br />
// will <span>return</span> the SAME instance <span>if</span> you call it twice during the</span><span><br />
// same HTTP request.</span><br />
<span>private</span> <span>static</span> MyDataContext Context<br />
{<br />
<span>get</span><br />
{<br />
<span>if</span> (HttpContext.Current.Items.Contains(DataAccessLayer._eelsContextKey) == <span>false</span>)<br />
{<br />
MyDataContext context = <span>new</span> MyDataContext(<span>&#8220;Connection String Here&#8230;&#8221;</span>);</p>
<p>HttpContext.Current.Items.Add(DataAccessLayer._eelsContextKey, context);</p>
<p><span>return</span> context;<br />
}<br />
<span>else</span><br />
{<br />
<span>return</span> (MyDataContext)HttpContext.Current.Items[DataAccessLayer._eelsContextKey];<br />
}<br />
}<br />
}<span></p>
<p>// Now we can make very simple DataAccessLayer functions like these:</span></p>
<p><span>public</span> <span>static</span> IEnumerable GetClients()<br />
{<br />
<span>return</span> <span>from</span> myRow <span>in</span> DataAccessLayer.Context.Clients <span>select</span> myRow;<br />
}</p>
<p><span>public</span> <span>static</span> IEnumerable GetProjects()<br />
{<br />
<span>return</span> <span>from</span> myRow <span>in</span> DataAccessLayer.Context.Projects <span>select</span> myRow;<br />
}</p>
<p><span>public</span> <span>static</span> IEnumerable GetThePoint()<br />
{<br />
<span>return</span> <span>from</span> myRow <span>in</span> DataAccessLayer.Context.You_Get_The_Point <span>select</span> myRow;<br />
}<br />
}</div>
<p>Let&#8217;s now look at the last area of performance gain from using LINQ that we will discuss.</p>
<h3>Redundant Update Checking</h3>
<p>The last place where you can see performance gains is with writing back to your data source. The LINQ data context not only remembers if you have already queried some data before, but it also keeps track of any changes that are made. This means that if you call the &#8220;save&#8221; function, but no changes were made first, then LINQ will ignore the request to save. Likewise, if you only change one property (such as &#8220;FirstName&#8221;), then LINQ will build an UPDATE statement that only contains that one column (again saving time and bandwidth).</p>
<h3>That&#8217;s a Wrap!</h3>
<p>New technologies may sometimes seem dauting (or even pointless). Hopefully this article has helped you to appreciate the clear benefits of using LINQ. Download Visual Studio Orcas (beta 2 at this point) and try out some of the examples in this article. I hope you grow to love LINQ as I have.</p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ&amp;bodytext=While%20the%20title%20of%20this%20article%20may%20seem%20misleading%2C%20you%20actually%20can%20achieve%20incredible%20performance%20gains%20by%20updating%20your%20web%20application%20to%20use%20LINQ.%20This%20article%20will%20dispell%20some%20common%20myths%20about%20LINQ%20and%20demonstrate%20how%20LINQ%20can%20practically%20i" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ&amp;notes=While%20the%20title%20of%20this%20article%20may%20seem%20misleading%2C%20you%20actually%20can%20achieve%20incredible%20performance%20gains%20by%20updating%20your%20web%20application%20to%20use%20LINQ.%20This%20article%20will%20dispell%20some%20common%20myths%20about%20LINQ%20and%20demonstrate%20how%20LINQ%20can%20practically%20i" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;t=Improving%20Performance%20With%20LINQ" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ&amp;annotation=While%20the%20title%20of%20this%20article%20may%20seem%20misleading%2C%20you%20actually%20can%20achieve%20incredible%20performance%20gains%20by%20updating%20your%20web%20application%20to%20use%20LINQ.%20This%20article%20will%20dispell%20some%20common%20myths%20about%20LINQ%20and%20demonstrate%20how%20LINQ%20can%20practically%20i" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;Title=Improving%20Performance%20With%20LINQ" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Improving%20Performance%20With%20LINQ&amp;u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;headline=Improving%20Performance%20With%20LINQ&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=While%20the%20title%20of%20this%20article%20may%20seem%20misleading%2C%20you%20actually%20can%20achieve%20incredible%20performance%20gains%20by%20updating%20your%20web%20application%20to%20use%20LINQ.%20This%20article%20will%20dispell%20some%20common%20myths%20about%20LINQ%20and%20demonstrate%20how%20LINQ%20can%20practically%20i" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;bm_description=Improving%20Performance%20With%20LINQ&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;t=Improving%20Performance%20With%20LINQ" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Improving%20Performance%20With%20LINQ&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;h=Improving%20Performance%20With%20LINQ" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Improving%20Performance%20With%20LINQ&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;story_title=Improving%20Performance%20With%20LINQ" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;title=Improving%20Performance%20With%20LINQ" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Improving%20Performance%20With%20LINQ%20-%20http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fimproving-performance-with-linq%2F&amp;submitHeadline=Improving%20Performance%20With%20LINQ&amp;submitSummary=While%20the%20title%20of%20this%20article%20may%20seem%20misleading%2C%20you%20actually%20can%20achieve%20incredible%20performance%20gains%20by%20updating%20your%20web%20application%20to%20use%20LINQ.%20This%20article%20will%20dispell%20some%20common%20myths%20about%20LINQ%20and%20demonstrate%20how%20LINQ%20can%20practically%20i&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/database-programming/improving-performance-with-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learn The Basics Of LINQ</title>
		<link>http://www.comteken.com/database-programming/learn-the-basics-of-linq/</link>
		<comments>http://www.comteken.com/database-programming/learn-the-basics-of-linq/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 15:48:50 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Database Programming]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=28</guid>
		<description><![CDATA[<a href="http://www.comteken.com/database-programming/learn-the-basics-of-linq/"><img align="left" hspace="5" width="100" height="100" src="http://www.comteken.com/wp-content/plugins/thumbnail-for-excerpts/tfe_no_thumb.png" class="alignleft wp-post-image tfe" alt="" title="" /></a>LINQ (Language INtegrated Query) is a powerful but misunderstood new language feature brought to us in the .NET framework version 3.5 (C# 3.0 and VB 9). Even though this is a new feature, it already has some huge misconceptions (such as thinking LINQ is a replacement to SQL). This article will teach you how to [...]]]></description>
			<content:encoded><![CDATA[<p>LINQ (Language INtegrated Query) is a powerful but misunderstood new language feature brought to us in the .NET framework version 3.5 (C# 3.0 and VB 9). Even though this is a new feature, it already has some huge misconceptions (such as thinking LINQ is a replacement to SQL). This article will teach you how to use LINQ (the language itself), where LINQ can save you a lot of time and will cover some basic concepts of Extension Methods and Lambda expressions.</p>
<h3>The Need for LINQ</h3>
<p>At first, when I didn&#8217;t understand LINQ, I couldn&#8217;t appreciate why we needed (or why anyone wanted) it. LINQ seemed very limited in usefulness, and I, like many of my colleagues, bashed it in ignorance. Now, due to understanding the semantics and benefits of the language, I&#8217;ve come to love it and wonder how I lived without it.</p>
<p>LINQ does not allow us to do anything new, as far as functionality goes. Really, if you wanted to make the argument of a &#8216;real programmer&#8217;, you could do all of this same functionality by writing encyclopedias of C code (heck, might as well just write it in assembly for &#8220;performance&#8221; reasons). But, LINQ does create a new way of looking at enumerations of data, and gives out-of-the-box functionality for them.</p>
<p>I realize that the above paragraphs might seem vague, so I&#8217;ll get right into real examples that I have used as well as some odd examples that you might not have thought about as being &#8216;queryable objects&#8217;.</p>
<h3>Baby Steps &#8211; Basic LINQ Queries</h3>
<p>Let&#8217;s look at some very simple examples of LINQ so that we can have a basis to grow on. Let&#8217;s pretend that we have a user control that has a variable amount of TextBoxes added to it (based on some database query or whatever). Our user control might have other controls (Buttons, Literals and the like), and we want to disable all of the TextBoxes if the user doesn&#8217;t have security permissions to edit these controls. Please keep in mind that this is a hypothetical scenario at this point just so we can see some basic LINQ (in this case, LINQ to Objects).</p>
<p>Now, you can do this a few ways. Because the number of TextBoxes is dynamic we would possibly do something like this:</p>
<div><span>// Loop through all of the controls.</span><br />
<span>foreach</span> (Control myChildControl <span>in</span> <span>this</span>.Controls)<br />
{<span><br />
// Make sure the control <span>is</span> a TextBox.</span><br />
<span>if</span> (myChildControl <span>is</span> TextBox)<br />
{<span><br />
// Cast that <span>&#8220;Control&#8221;</span> <span>object</span> to a TextBox and disable it.</span><br />
((TextBox)myChildControl).Enabled = <span>false</span>;<br />
}<br />
}</div>
<p>The above code isn&#8217;t really that bad, but I for one don&#8217;t like that I have to loop through all of my controls, check the type, cast it to the type that I just checked for, and then set my property. So, let&#8217;s see how LINQ will not only &#8220;pretty up&#8221; my code, but will also make more sense to us programmers:</p>
<div><span>// Define a enumeration only containing child TextBoxes.</span><br />
<span>var</span> myChildTextBoxes = <span>from</span> myChildControl <span>in</span> <span>this</span>.Controls<br />
<span>where</span> myChildControl <span>is</span> TextBox<br />
<span>select</span> myChildControl;<span></p>
<p>// Loop through my TextBoxes.</span><br />
<span>foreach</span> (TextBox myChildTextBox <span>in</span> myChildTextBoxes)<br />
{<span><br />
// Disable the TextBox.</span><br />
myChildTextBox.Enabled = <span>false</span>;<br />
}</div>
<p>This LINQ approach is much cleaner. It&#8217;s clear from the start that you want to work with a list of TextBoxes only. Secondly, you aren&#8217;t type checking, then re-type casting. Now, you might argue that you could avoid the second type-cast by using an &#8220;as&#8221; keyword and checking for nulls, or you could say that you don&#8217;t like how many lines of code this takes. Those are some valid claims, but if you are thinking that, then apparently you have a short attention span (as I mentioned above that this is just to get you a little familiar with the language syntax).</p>
<h3>Step Two &#8211; Querying Unconventional Data Sources</h3>
<p>Once you begin to use LINQ a bit more in you&#8217;re C# (or VB), you&#8217;ll begin to think of &#8216;data sources&#8217; in a different way. For instance, you may see a directory (a folder on your computer) as a data source of files. You can even start to look at your list of running processes as a data source. Really, no enumeration is safe!</p>
<p>What if you were to make a web application that allowed you to upload and download files from your home machine? Let&#8217;s say you had a DropDownList that would show a list of the files in the specified directory, perhaps with a button that lets you download it. How would you go about populating that DropDownList with clean file names for the &#8220;Text&#8221; property and perhaps the path for the &#8220;Value&#8221; property? I&#8217;ll go a step further and say that this is what you&#8217;re DropDownList looks like in ASP.NET:</p>
<div>&lt;<span>asp:DropDownList</span> <span>ID</span><span>=&#8221;MyFilesList&#8221;</span> <span>runat</span><span>=&#8221;server&#8221;</span><br />
<span>DataTextField</span><span>=&#8221;FileName&#8221;</span> <span>DataValueField</span><span>=&#8221;FullPath&#8221;</span> /&gt;</div>
<p>Well, you could create you&#8217;re own custom class that has a FileName property and a FullPath property. Then you could make a generic list of that class, loop through the files in a Directory.GetFiles method, add an entry for each file, set the list as the DataSource of the DropDownList and data bind. That&#8217;s a whole lot of nasty that I&#8217;m not even going to show the code for (really, because I never want to write nasty code like that again). Here&#8217;s what I would do using LINQ:</p>
<div><span>// Set the DataSource of our DropDownList to an enumeration of a <span>new</span></span><span><br />
// <span>&#8220;anonymous type&#8221;</span> (meaning a <span>class</span> that <span>is</span> being built <span>on</span> the fly)</span><span><br />
// that contains the file names and full paths <span>in</span> our directory.</span><br />
<span>this</span>.MyFilesList.DataSource = <span>from</span> file <span>in</span> Directory.GetFiles(somePath)<br />
<span>select</span> <span>new</span><br />
{<br />
FileName = Path.GetFileName(file),<br />
FullPath = file<br />
};<span></p>
<p>// Call the <span>&#8220;DataBind&#8221;</span> method <img src='http://www.comteken.com/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </span><br />
<span>this</span>.MyFilesList.DataBind();</div>
<p>All I can say after that is &#8220;Go LINQ GO!&#8221; and of course &#8220;case closed!&#8221;</p>
<h3>Extension Methods and Lambda Expressions</h3>
<p>One more part to LINQ is the heavy use of Extension Methods and Lambda expressions to accomplish this beautiful language enhancement. If you remember our original example of disabling the TextBoxes, then you&#8217;re going to love one of the 50 STANDARD OPERATORS that are brought to you just by including a reference to the System.Linq namespace! Check this out:</p>
<div><span>// Loop through my TextBoxes.</span><br />
<span>foreach</span> (TextBox myChildTextBox <span>in</span> <span>this</span>.Controls.OfType&lt;TextBox&gt;())<br />
{<span><br />
// Disable the TextBox.</span><br />
myChildTextBox.Enabled = <span>false</span>;<br />
}</div>
<p>Don&#8217;t check you&#8217;re MSDN&#8230; there is no method called &#8220;OfType&#8221; on the ControlCollection class. But, because the ControlCollection class implements the IEnumerable interface, it automatically gets all of the extension methods that LINQ has kindly given to any enumeration (such as &#8220;OfType&#8221; which I am using above).</p>
<p>LINQ can be done one of two ways (or a combination of both): query expressions or method expressions. The difference between the two is only a syntax difference, as they ultimately compile into the same thing. Example:</p>
<div><span>// This line of code&#8230;</span><br />
<span>var</span> textBoxes = <span>from</span> control <span>in</span> <span>this</span>.Controls<br />
<span>where</span> control <span>is</span> TextBox <span>select</span> control;<span></p>
<p>// <span>is</span> the same thing <span>as</span> <span>this</span>.</span><br />
<span>var</span> textBoxes = <span>this</span>.Controls.OfType&lt;TextBox&gt;();</div>
<p>Lambda expressions come into play when you want to perform another standard operation such as &#8220;SUM&#8221; or &#8220;MAX&#8221;. Here&#8217;s an example that I have used recently. I wanted to make a property that displayed the &#8220;TotalBill&#8221; of an object that had child &#8220;orders&#8221; in it. Here&#8217;s how lambda helped out:</p>
<div><span>public</span> <span>decimal</span> TotalBill<br />
{<br />
<span>get</span><br />
{<span><br />
// Keep <span>in</span> mind that <span>&#8220;Orders&#8221;</span> <span>is</span> just a list of</span><span><br />
// &#8216;Order&#8217; objects and has no special functionality.</span><br />
<span>return</span> <span>this</span>.Orders.Sum(order =&gt; order.Price);<br />
}<br />
}</div>
<p>If you&#8217;re not quite familiar with lambda as of yet, you can get a better explaination of the above in a blog post I made not long ago: LINQ, Lamda and Extension Methods Work Together!</p>
<h3>Conclusion</h3>
<p>There is much more to say about LINQ, but I hope for now you have a basic understanding and appreciation for it. As I get more time, I&#8217;ll talk about some deeper subjects with LINQ such as how to use it in an enterprise level application and how to gain performance and improve multi-developer cohesion with it.</p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ&amp;bodytext=LINQ%20%28Language%20INtegrated%20Query%29%20is%20a%20powerful%20but%20misunderstood%20new%20language%20feature%20brought%20to%20us%20in%20the%20.NET%20framework%20version%203.5%20%28C%23%203.0%20and%20VB%209%29.%20Even%20though%20this%20is%20a%20new%20feature%2C%20it%20already%20has%20some%20huge%20misconceptions%20%28such%20as%20thinking%20LINQ" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ&amp;notes=LINQ%20%28Language%20INtegrated%20Query%29%20is%20a%20powerful%20but%20misunderstood%20new%20language%20feature%20brought%20to%20us%20in%20the%20.NET%20framework%20version%203.5%20%28C%23%203.0%20and%20VB%209%29.%20Even%20though%20this%20is%20a%20new%20feature%2C%20it%20already%20has%20some%20huge%20misconceptions%20%28such%20as%20thinking%20LINQ" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;t=Learn%20The%20Basics%20Of%20LINQ" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ&amp;annotation=LINQ%20%28Language%20INtegrated%20Query%29%20is%20a%20powerful%20but%20misunderstood%20new%20language%20feature%20brought%20to%20us%20in%20the%20.NET%20framework%20version%203.5%20%28C%23%203.0%20and%20VB%209%29.%20Even%20though%20this%20is%20a%20new%20feature%2C%20it%20already%20has%20some%20huge%20misconceptions%20%28such%20as%20thinking%20LINQ" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;Title=Learn%20The%20Basics%20Of%20LINQ" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Learn%20The%20Basics%20Of%20LINQ&amp;u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;headline=Learn%20The%20Basics%20Of%20LINQ&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=LINQ%20%28Language%20INtegrated%20Query%29%20is%20a%20powerful%20but%20misunderstood%20new%20language%20feature%20brought%20to%20us%20in%20the%20.NET%20framework%20version%203.5%20%28C%23%203.0%20and%20VB%209%29.%20Even%20though%20this%20is%20a%20new%20feature%2C%20it%20already%20has%20some%20huge%20misconceptions%20%28such%20as%20thinking%20LINQ" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;bm_description=Learn%20The%20Basics%20Of%20LINQ&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;t=Learn%20The%20Basics%20Of%20LINQ" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Learn%20The%20Basics%20Of%20LINQ&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;h=Learn%20The%20Basics%20Of%20LINQ" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Learn%20The%20Basics%20Of%20LINQ&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;story_title=Learn%20The%20Basics%20Of%20LINQ" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;title=Learn%20The%20Basics%20Of%20LINQ" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Learn%20The%20Basics%20Of%20LINQ%20-%20http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Flearn-the-basics-of-linq%2F&amp;submitHeadline=Learn%20The%20Basics%20Of%20LINQ&amp;submitSummary=LINQ%20%28Language%20INtegrated%20Query%29%20is%20a%20powerful%20but%20misunderstood%20new%20language%20feature%20brought%20to%20us%20in%20the%20.NET%20framework%20version%203.5%20%28C%23%203.0%20and%20VB%209%29.%20Even%20though%20this%20is%20a%20new%20feature%2C%20it%20already%20has%20some%20huge%20misconceptions%20%28such%20as%20thinking%20LINQ&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/database-programming/learn-the-basics-of-linq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Self Sorting GridView with LINQ Expression Trees</title>
		<link>http://www.comteken.com/database-programming/self-sorting-gridview-with-linq-expression-trees/</link>
		<comments>http://www.comteken.com/database-programming/self-sorting-gridview-with-linq-expression-trees/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 15:47:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Database Programming]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=26</guid>
		<description><![CDATA[<a href="http://www.comteken.com/database-programming/self-sorting-gridview-with-linq-expression-trees/"><img align="left" hspace="5" width="100" height="100" src="http://www.comteken.com/wp-content/plugins/thumbnail-for-excerpts/tfe_no_thumb.png" class="alignleft wp-post-image tfe" alt="" title="" /></a>The GridView control in ASP.NET 2.0 is lacking some major (yet common) functionality &#8211; sorting. This article will show you how to create a &#8220;self-sorting&#8221; GridView using LINQ expression trees. First, it&#8217;s important to explain what I mean when I say that the GridView doesn&#8217;t have sorting capabilities. You&#8217;re probably saying to yourself &#8211; &#8220;I [...]]]></description>
			<content:encoded><![CDATA[<p>The GridView control in ASP.NET 2.0 is lacking some major (yet common) functionality &#8211; sorting. This article will show you how to create a &#8220;self-sorting&#8221; GridView using LINQ expression trees.</p>
<p>First, it&#8217;s important to explain what I mean when I say that the GridView doesn&#8217;t have sorting capabilities. You&#8217;re probably saying to yourself &#8211; &#8220;I know the GridView has sorting built in because it has a &#8216;Sort&#8217; method and &#8216;Sorting&#8217; and &#8216;Sorted&#8217; events.&#8221; And that&#8217;s true, but those are merely placeholders that allow you to write your own sorting code.</p>
<p>Also, if you use the designer to build your GridViews, and bind them to SQL data sources, then you might also be confused about the above statements. But rest assured that the sorting magic is all happening in the terribly inefficient SqlDataSource control, and not the actual GridView itself.</p>
<p>If you&#8217;re not already familiar with the basics of LINQ, then you might want to first read this article: Learn The Basics of LINQ. To sum up LINQ in one sentence (for the purpose of moving on) I would say: &#8220;LINQ is a language feature in .NET that provides a ton of out-of-the-box functionality for querying data.&#8221;</p>
<h3>The Problem &#8211; Why The GridView Can&#8217;t Sort</h3>
<p>So why is it that the GridView control doesn&#8217;t have the ability to sort by default? The problem lies in the fact that the data source is very dynamic, could be of any type and could even be just a paged subset of the ultimate source.</p>
<p>Another problem is performance. If your data source was from a SQL table, then you would want to pass the sorting functionality on to the SQL Server itself to gain the most performance. Because the GridView doesn&#8217;t know what your source will be, it can&#8217;t really provide the needed functionality.</p>
<h3>The Solution &#8211; LINQ</h3>
<p>Since LINQ provides querying capabilities on a generic level, the above problems are no problem. Because LINQ to objects can handle *any* enumerable objects, and LINQ to SQL is smart enough to pass the sorting logic on to SQL Server, you win no matter what.</p>
<p>For example, imagine if the data source was a collection of MembershipUsers (via the Membership.GetUsers method). Here&#8217;s how easy it would be to sort using LINQ and the OrderBy method:</p>
<div><span>// Using LINQ to sort by a user&#8217;s name.</span><br />
myGridView.DataSource = Membership.GetUsers()<br />
.OfType&lt;MembershipUser&gt;()<br />
.OrderBy(user =&gt; user.Name);</div>
<p>That was very easy to do, but that&#8217;s because we know the data type of our source at compile time. But if we are going to make a generic &#8220;self sorting GridView&#8221;, then we have to build our functionality with the limitation of not knowing the data type of the data source or the name of the field that is being sorted. So we&#8217;ll need to reconstruct this functionality at runtime by building &#8220;expression trees&#8221; (the power behind LINQ).</p>
<h3>Step One &#8211; Build Our GridView Control</h3>
<p>To begin with, let&#8217;s build a basic control that inherits from GridView , and stub out a placeholder to put our sorting logic.</p>
<div><span>public</span> <span>class</span> SelfSortingGridView : GridView<br />
{<br />
<span>protected</span> <span>override</span> <span>void</span> OnSorting(GridViewSortEventArgs e)<br />
{<span><br />
// Our code below <span>is</span> going to live here&#8230;</span><br />
}<br />
}</div>
<p>Now that we have our custom GridView control ready to go, we can move on to step two.</p>
<h3>Step Two &#8211; Creating an Expression to Sort With</h3>
<p>As was stated earlier, we need to build an expression that will tell LINQ how we want to sort our data source. There are a few tricky issues here that we need to identify and overcome. If we try to copy the example found in this blog post to reproduce our sorting example at the top of this article, we would get half-way there:</p>
<div><span>// Using a custom LINQ expression to sort by a user&#8217;s name.</span><br />
<span>var</span> param = Expression.Parameter(<span>typeof</span>(MembershipUser), <span>&#8220;user&#8221;</span>);<span></p>
<p>// By the way, <span>if</span> you don&#8217;t <span>&#8220;box&#8221;</span> the <span>return</span> type <span>into</span> an <span>object</span>,</span><span><br />
// you&#8217;ll <span>get</span> a runtime error when you <span>try</span> to sort by a value type.</span><span><br />
// So instead of doing <span>this</span> (which would be expected):</span><br />
<span>var</span> sortExpression = Expression.Lambda&lt;Func&lt;MembershipUser, <span>object</span>&gt;<br />
(Expression.Property(param, <span>&#8220;Name&#8221;</span>), param);<span></p>
<p>// I&#8217;m going to <span>do</span> <span>this</span>:</span><br />
<span>var</span> sortExpression = Expression.Lambda&lt;Func&lt;MembershipUser, <span>object</span>&gt;<br />
(Expression.Convert(Expression.Property(param, <span>&#8220;Name&#8221;</span>), <span>typeof</span>(<span>object</span>)), param);<span></p>
<p>// And then I can sort my data source:</span><br />
myGridView.DataSource = Membership.GetUsers()<br />
.OfType&lt;MembershipUser&gt;().OrderBy(sortExpression);</div>
<p>So we&#8217;ve solved one issue &#8211; that of finding the property to sort with by it&#8217;s name. The &#8220;Expression.Property&#8221; method above did that for us. Now, we have to solve the second issue &#8211; not knowing what the data type of the data source will be until runtime. To solve this in the fewest lines of code, I&#8217;m going to make a custom generic class that will create the sort expression for us so that we can simply supply the type at runtime.</p>
<div><span>public</span> <span>class</span> GenericSorter&lt;T&gt;<br />
{<br />
<span>public</span> IEnumerable&lt;T&gt; Sort(IEnumerable&lt;T&gt; source, <span>string</span> sortBy)<br />
{<br />
<span>var</span> param = Expression.Parameter(<span>typeof</span>(T), <span>&#8220;item&#8221;</span>);</p>
<p><span>var</span> sortExpression = Expression.Lambda&lt;Func&lt;T, <span>object</span>&gt;&gt;<br />
(Expression.Convert(Expression.Property(param, sortBy), <span>typeof</span>(<span>object</span>)), param);</p>
<p><span>return</span> source.AsQueryable&lt;T&gt;().OrderBy&lt;T, <span>object</span>&gt;(sortExpression);<br />
}<br />
}</div>
<p>Beautiful! Notice that because we are using a generic class, we can tell the compiler that we want to use the type &#8220;T&#8221; (whatever &#8220;T&#8221; will be at runtime, we don&#8217;t care)! So, if I wanted to create an instance of my GenericSorter class at runtime, passing in a type that I won&#8217;t know until runtime, and then call the &#8220;Sort&#8221; method, I would just do some simple reflection as will be shown below.</p>
<p>For simplicities sake, the data source is stored in a variable called &#8220;_data&#8221;. You can download the whole source code at the end of the article to see exactly how it&#8217;s done. Here&#8217;s a sample of the code:</p>
<div><span>// Give me the data type of the GridView&#8217;s data source.</span><br />
Type dataSourceType = _data.GetType();<span></p>
<p>// Determine the data type of the items <span>in</span> the data source at runtime.</span><br />
Type dataItemType = <span>typeof</span>(<span>object</span>);</p>
<p><span>if</span> (dataSourceType.HasElementType)<br />
{<span><br />
// Get the element type <span>if</span> the data source <span>is</span> an array.</span><br />
dataItemType = dataSourceType.GetElementType();<br />
}<br />
<span>else</span> <span>if</span> (dataSourceType.IsGenericType)<br />
{<span><br />
// Get the element type <span>if</span> the data source <span>is</span> a generic list.</span><br />
dataItemType = dataSourceType.GetGenericArguments()[0];<br />
}<span></p>
<p>// Create an instance of the GenericSorter <span>class</span> passing <span>in</span> the data item type.</span><br />
Type sorterType = <span>typeof</span>(GenericSorter&lt;&gt;).MakeGenericType(dataItemType);</p>
<p><span>var</span> sorterObject = Activator.CreateInstance(sorterType);<span></p>
<p>// Now I can call the <span>&#8220;Sort&#8221;</span> method passing <span>in</span> my runtime types.</span><br />
<span>this</span>.DataSource = sorterType.GetMethod(<span>&#8220;Sort&#8221;</span>, <span>new</span> Type[] { dataSourceType, <span>typeof</span>(<span>string</span>) })<br />
.Invoke(sorterObject, <span>new</span> <span>object</span>[] { _data, e.SortExpression });</p>
<p><span>this</span>.DataBind();</div>
<h3>Conclusion</h3>
<p>At first glance, it may seem like we&#8217;re doing a lot of code just for sorting functionality. You may even be thinking that this is an extremely convoluted way of sorting a GridView, but really it&#8217;s not. Once you understand what LINQ is doing, and how to build your own expression trees, you&#8217;ll really appreciate this new language feature in .NET.</p>
<p>I&#8217;ll include a web project that has the full source code and some examples of different objects that can be sorted here. If you first don&#8217;t understand what all is going on, I&#8217;d recommend that you download the source code and step through it in the debugger.</p>
<p>I hope this article has excited you about LINQ and expression trees. If you&#8217;re not excited, I hope your informed. If you don&#8217;t feel informed, I hope at least that you&#8217;re not now afraid of LINQ.</p>
<p>Here&#8217;s the source code and sample project: <a href="http://www.singingeels.com/Articles/Articles/UserFile.aspx?FileID=d88d86f6-08e1-40fc-b908-2b4584036099">SingingEels_LinqSelfSortingGridView.zip</a></p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;bodytext=The%20GridView%20control%20in%20ASP.NET%202.0%20is%20lacking%20some%20major%20%28yet%20common%29%20functionality%20-%20sorting.%20This%20article%20will%20show%20you%20how%20to%20create%20a%20%22self-sorting%22%20GridView%20using%20LINQ%20expression%20trees.%0D%0A%0D%0AFirst%2C%20it%27s%20important%20to%20explain%20what%20I%20mean%20when%20I%20say" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;notes=The%20GridView%20control%20in%20ASP.NET%202.0%20is%20lacking%20some%20major%20%28yet%20common%29%20functionality%20-%20sorting.%20This%20article%20will%20show%20you%20how%20to%20create%20a%20%22self-sorting%22%20GridView%20using%20LINQ%20expression%20trees.%0D%0A%0D%0AFirst%2C%20it%27s%20important%20to%20explain%20what%20I%20mean%20when%20I%20say" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;t=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;annotation=The%20GridView%20control%20in%20ASP.NET%202.0%20is%20lacking%20some%20major%20%28yet%20common%29%20functionality%20-%20sorting.%20This%20article%20will%20show%20you%20how%20to%20create%20a%20%22self-sorting%22%20GridView%20using%20LINQ%20expression%20trees.%0D%0A%0D%0AFirst%2C%20it%27s%20important%20to%20explain%20what%20I%20mean%20when%20I%20say" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;Title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;headline=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=The%20GridView%20control%20in%20ASP.NET%202.0%20is%20lacking%20some%20major%20%28yet%20common%29%20functionality%20-%20sorting.%20This%20article%20will%20show%20you%20how%20to%20create%20a%20%22self-sorting%22%20GridView%20using%20LINQ%20expression%20trees.%0D%0A%0D%0AFirst%2C%20it%27s%20important%20to%20explain%20what%20I%20mean%20when%20I%20say" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;bm_description=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;t=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;h=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;story_title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;title=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees%20-%20http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fself-sorting-gridview-with-linq-expression-trees%2F&amp;submitHeadline=Self%20Sorting%20GridView%20with%20LINQ%20Expression%20Trees&amp;submitSummary=The%20GridView%20control%20in%20ASP.NET%202.0%20is%20lacking%20some%20major%20%28yet%20common%29%20functionality%20-%20sorting.%20This%20article%20will%20show%20you%20how%20to%20create%20a%20%22self-sorting%22%20GridView%20using%20LINQ%20expression%20trees.%0D%0A%0D%0AFirst%2C%20it%27s%20important%20to%20explain%20what%20I%20mean%20when%20I%20say&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/database-programming/self-sorting-gridview-with-linq-expression-trees/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending LINQ &#8211; Specifying a Property in the Distinct Function</title>
		<link>http://www.comteken.com/database-programming/extending-linq-specifying-a-property-in-the-distinct-function/</link>
		<comments>http://www.comteken.com/database-programming/extending-linq-specifying-a-property-in-the-distinct-function/#comments</comments>
		<pubDate>Thu, 29 Oct 2009 15:26:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Database Programming]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://www.comteken.com/?p=21</guid>
		<description><![CDATA[<a href="http://www.comteken.com/database-programming/extending-linq-specifying-a-property-in-the-distinct-function/"><img align="left" hspace="5" width="100" height="100" src="http://www.comteken.com/wp-content/plugins/thumbnail-for-excerpts/tfe_no_thumb.png" class="alignleft wp-post-image tfe" alt="" title="" /></a>The &#8220;Distinct&#8221; function in LINQ is definitely one of the top 10 most used functions, but it&#8217;s probably one of the top 10 most incomplete as well. This article will show how to extend the IEnumerable interface to allow a very easy way to specify what property makes your list unique (or &#8216;distinct&#8217;). If you [...]]]></description>
			<content:encoded><![CDATA[<p>The &#8220;Distinct&#8221; function in LINQ is definitely one of the top 10 most used functions, but it&#8217;s probably one of the top 10 most incomplete as well. This article will show how to extend the IEnumerable interface to allow a very easy way to specify what property makes your list unique (or &#8216;distinct&#8217;).</p>
<p>If you are already familiar with the standard query operators in LINQ, then you have no doubt tried to do something like this:</p>
<div><span>// Make a unique list of customers <span>from</span> the big list!</span><br />
<span>var</span> uniqueList = bigList.Distinct(item =&gt; item.ID);<span></p>
<p>// Do something with the unique customers&#8230;</span></div>
<p>The problem is, the &#8220;Distinct&#8221; function doesn&#8217;t let you specify a property by which you call your object &#8216;unique&#8217;. In other words, if you have a collection that has the same customer data in there twice&#8230; calling &#8220;Distinct&#8221; will not remove the duplicate. Note &#8211; this statement requires the following VERY IMPORTANT DISCLAIMER:</p>
<div>
<p>It&#8217;s highly important that you read what I just said above correctly. I said &#8220;the same customer *data*&#8221;&#8230; I did NOT say &#8220;the same customer *object*.&#8221; Why is that significant? &#8211; You will learn the answer to that question further in the article.</p></div>
<h3>How Does LINQ Implement the Distinct Function</h3>
<p>Before we can solve the problem explained above by extending LINQ, we have to understand why the method currently does not remove duplicates in all cases. To understand that, we have to understand how it is removing duplicates at all. &#8211; Hopefully I haven&#8217;t lost you yet <img src='http://www.comteken.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To explain in short &#8211; the Distinct function will iterate through an IEnumerable&lt;T&gt; one item at a time and perform the following pseudo-code:</p>
<ol>
<li>Get the hash code of the current item using the GetHashCode() method which *all* objects in the .NET Framework have.</li>
<li>Check to see if this hash code has been seen before (by checking in a private dictionary hidden from our eyes).</li>
<li>If the hash code is not already in the dictionary, then we simply add the current item to the dictionary and return the object.</li>
<li>If that very same hash code has already been found, then we compare the current item with the last item with that hash to double check uniqueness and return the object if it is unique.</li>
</ol>
<p>Here is what that code *could* look like (but it&#8217;s not exact):</p>
<div><span>var</span> hashedItems = <span>new</span> Dictionary&lt;<span>int</span>, T&gt;();</p>
<p><span>foreach</span> (T item <span>in</span> <span>this</span>.Items)<br />
{<br />
<span>int</span> currentHash = item.GetHashCode();</p>
<p><span>if</span> (hashedItems.ContainsKey(currentHash) == <span>false</span>)<br />
{<br />
hashedItems.Add(currentHash, item);<span></p>
<p>// We&#8217;ve never seen <span>this</span> item before&#8230; <span>return</span> it!</span><br />
yield <span>return</span> item;<br />
}<br />
<span>else</span><br />
{<br />
<span>if</span> (item.Equals(hashedItems[currentHash]) == <span>false</span>)<br />
{<span><br />
// We thought we&#8217;ve seen <span>this</span> item, but guess not&#8230; <span>return</span> it!</span><br />
yield <span>return</span> item;<br />
}<br />
}<br />
}</div>
<p>By the way, did you catch the reason why two objects that represent the same data will not be de-duped? The reason is that step 1 is to use the &#8220;GetHashCode()&#8221; method to do a quick test. The here problem is that you may not consider these following objects to be different, but LINQ would:</p>
<div><span>var</span> personOne = <span>new</span> Person(123, <span>&#8220;Timothy&#8221;</span>, <span>&#8220;Khouri&#8221;</span>);<br />
<span>var</span> personTwo = <span>new</span> Person(123, <span>&#8220;Timothy&#8221;</span>, <span>&#8220;Khouri&#8221;</span>);</div>
<p>The reason why those two objects would be considered &#8216;different&#8217; is because different instances of an object will have different hash codes, even if the &#8216;data&#8217; is the same. Now that we understand enough of what LINQ is doing, we can extend it to meet our simple needs.</p>
<h3>Extending IEnumerable&lt;T&gt;</h3>
<p>So, how do we add our functionality to everything that LINQ is already extending? &#8211; Very easily, extend the only interface that LINQ is extending! As a side note, I recommend putting your code in the &#8220;System.Collections.Generic&#8221; namespace so that once you add a &#8216;using&#8217; (or &#8216;Imports&#8217; in VB) to that namespace, you&#8217;ll automatically tap into your extensions too.</p>
<div><span>// I feel like I&#8217;m working <span>for</span> Microsoft when I use <span>this</span> <span>namespace</span>&#8230;</span><span><br />
// will I <span>get</span> paid <span>for</span> <span>this</span> article? &#8211; Probably not <img src='http://www.comteken.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </span><br />
<span>namespace</span> System.Collections.Generic<br />
{<br />
<span>public</span> <span>static</span> <span>class</span> MyIEnumerableExtensions<br />
{<br />
<span>public</span> <span>static</span> IEnumerable&lt;T&gt; Distinct(<span>this</span> IEnumerable&lt;T&gt; source,<br />
Func&lt;T, <span>object</span>&gt; uniqueCheckerMethod)<br />
{<span><br />
// Extension code here&#8230;</span><br />
}<br />
}<br />
}</div>
<p>So, now that we have our method stub that extends any list (array or collection) by letting the developer specify how he considers an object to be unique, we need to fill in the contents of our function.</p>
<p>Because the &#8220;Distinct&#8221; LINQ extension has an overload that accepts an &#8216;IEqualityComparer&#8217; object, we already have half of the code written for us! So, what we are going to do is create a generic &#8216;comparer&#8217; class and use that to do our filtering. Here is what our extension will look like:</p>
<div><span>public</span> <span>static</span> IEnumerable&lt;T&gt; Distinct(<span>this</span> IEnumerable&lt;T&gt; source,<br />
Func&lt;T, <span>object</span>&gt; uniqueCheckerMethod)<br />
{<br />
<span>return</span> source.Distinct(<span>new</span> GenericComparer&lt;T&gt;(uniqueCheckerMethod));<br />
}</div>
<p>And, our &#8220;GenericComparer&lt;T&gt;&#8221; class will be as follows:</p>
<div><span>public</span> <span>class</span> GenericComparer&lt;T&gt; : IEqualityComparer&lt;T&gt;<br />
{<br />
<span>public</span> GenericComparer(Func&lt;T, <span>object</span>&gt; uniqueCheckerMethod)<br />
{<br />
<span>this</span>._uniqueCheckerMethod = uniqueCheckerMethod;<br />
}</p>
<p><span>private</span> Func&lt;T, <span>object</span>&gt; _uniqueCheckerMethod;</p>
<p><span>bool</span> IEqualityComparer&lt;T&gt;.Equals(T x, T y)<br />
{<br />
<span>return</span> <span>this</span>._uniqueCheckerMethod(x).Equals(<span>this</span>._uniqueCheckerMethod(y));<br />
}</p>
<p><span>int</span> IEqualityComparer&lt;T&gt;.GetHashCode(T obj)<br />
{<br />
<span>return</span> <span>this</span>._uniqueCheckerMethod(obj).GetHashCode();<br />
}<br />
}</div>
<p>Now, I can filter the following list like so:</p>
<div><span>var</span> people = <span>new</span> List&lt;Person&gt;();</p>
<p>people.Add(<span>new</span> Person(123, <span>&#8220;Timothy&#8221;</span>, <span>&#8220;Khouri&#8221;</span>));<br />
people.Add(<span>new</span> Person(124, <span>&#8220;Bob&#8221;</span>, <span>&#8220;Dole&#8221;</span>));<br />
people.Add(<span>new</span> Person(123, <span>&#8220;Timothy&#8221;</span>, <span>&#8220;Khouri&#8221;</span>));<br />
people.Add(<span>new</span> Person(125, <span>&#8220;Gill&#8221;</span>, <span>&#8220;Bates&#8221;</span>));</p>
<p><span>foreach</span> (<span>var</span> person <span>in</span> people.Distinct(p =&gt; p.ID))<br />
{<span><br />
// Only the 3 unique people will be displayed!</span><br />
Console.WriteLine(person.FullName);<br />
}</div>
<h3>Download the Source File</h3>
<p>While the source for this article is very simple (and is spread throughout the article), you may want to download it and test it out for yourself. This project (compiled for Visual Studio 2008 &#8211; .NET 3.5 SP1) has a simple Console app that demonstrates the code above. I encourage you to put some break-points in the code to see how it&#8217;s all working. Here&#8217;s the code: <a href="http://www.singingeels.com/Articles/Articles/UserFile.aspx?FileID=d30bcff1-6d7c-4a83-ba0e-58160583b44a">SingingEels_ExtendingLinqDistinct.zip</a></p>
<p>Source:singingeels.com</p>



Share and Enjoy:


	<a rel="nofollow"  target="_blank" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;bodytext=The%20%22Distinct%22%20function%20in%20LINQ%20is%20definitely%20one%20of%20the%20top%2010%20most%20used%20functions%2C%20but%20it%27s%20probably%20one%20of%20the%20top%2010%20most%20incomplete%20as%20well.%20This%20article%20will%20show%20how%20to%20extend%20the%20IEnumerable%20interface%20to%20allow%20a%20very%20easy%20way%20to%20specify%20what%20" title="Digg"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/digg.png" title="Digg" alt="Digg" /></a>
	<a rel="nofollow"  target="_blank" href="http://sphinn.com/index.php?c=post&amp;m=submit&amp;link=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F" title="Sphinn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/sphinn.png" title="Sphinn" alt="Sphinn" /></a>
	<a rel="nofollow"  target="_blank" href="http://delicious.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;notes=The%20%22Distinct%22%20function%20in%20LINQ%20is%20definitely%20one%20of%20the%20top%2010%20most%20used%20functions%2C%20but%20it%27s%20probably%20one%20of%20the%20top%2010%20most%20incomplete%20as%20well.%20This%20article%20will%20show%20how%20to%20extend%20the%20IEnumerable%20interface%20to%20allow%20a%20very%20easy%20way%20to%20specify%20what%20" title="del.icio.us"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.facebook.com/share.php?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;t=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="Facebook"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mixx.com/submit?page_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="Mixx"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/mixx.png" title="Mixx" alt="Mixx" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;annotation=The%20%22Distinct%22%20function%20in%20LINQ%20is%20definitely%20one%20of%20the%20top%2010%20most%20used%20functions%2C%20but%20it%27s%20probably%20one%20of%20the%20top%2010%20most%20incomplete%20as%20well.%20This%20article%20will%20show%20how%20to%20extend%20the%20IEnumerable%20interface%20to%20allow%20a%20very%20easy%20way%20to%20specify%20what%20" title="Google Bookmarks"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.blinklist.com/index.php?Action=Blink/addblink.php&amp;Url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;Title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="BlinkList"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/blinklist.png" title="BlinkList" alt="BlinkList" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.diigo.com/post?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="Diigo"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/diigo.png" title="Diigo" alt="Diigo" /></a>
	<a rel="nofollow"  target="_blank" href="http://cgi.fark.com/cgi/fark/farkit.pl?h=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F" title="Fark"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/fark.png" title="Fark" alt="Fark" /></a>
	<a rel="nofollow"  target="_blank" href="http://faves.com/Authoring.aspx?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="Faves"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/bluedot.png" title="Faves" alt="Faves" /></a>
	<a rel="nofollow"  target="_blank" href="http://laaik.it/NewStoryCompact.aspx?uri=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;headline=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;cat=5e082fcc-8a3b-47e2-acec-fdf64ff19d12" title="laaik.it"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/laaikit.png" title="laaik.it" alt="laaik.it" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;source=Tech+News%2C+Tech+Resources%2C+Technology+Articles%2C+Gadget+News%2C+Computer+News+IT+news%2C+software+technology%2C+IT+resources%2C+computer+software%2C+laptops%2C+desktops%2C++information+systems%2C++hardware+technology%2C+multimedia%2C+Windows+OS%2C+linux+clients%2C+network+solution%2C+easy+internet%2C+essential+guides%2C+domains%2C+webhosting%2C+web+program%2C+database+programming&amp;summary=The%20%22Distinct%22%20function%20in%20LINQ%20is%20definitely%20one%20of%20the%20top%2010%20most%20used%20functions%2C%20but%20it%27s%20probably%20one%20of%20the%20top%2010%20most%20incomplete%20as%20well.%20This%20article%20will%20show%20how%20to%20extend%20the%20IEnumerable%20interface%20to%20allow%20a%20very%20easy%20way%20to%20specify%20what%20" title="LinkedIn"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/linkedin.png" title="LinkedIn" alt="LinkedIn" /></a>
	<a rel="nofollow"  target="_blank" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="Live"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/live.png" title="Live" alt="Live" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.mister-wong.com/addurl/?bm_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;bm_description=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;plugin=soc" title="MisterWong"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/misterwong.png" title="MisterWong" alt="MisterWong" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.myspace.com/Modules/PostTo/Pages/?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;t=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="MySpace"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/myspace.png" title="MySpace" alt="MySpace" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvibes.com/share?title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F" title="Netvibes"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvibes.png" title="Netvibes" alt="Netvibes" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.netvouz.com/action/submitBookmark?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;popup=no" title="Netvouz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/netvouz.png" title="Netvouz" alt="Netvouz" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.newsvine.com/_tools/seed&amp;save?u=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;h=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="NewsVine"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/newsvine.png" title="NewsVine" alt="NewsVine" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.propeller.com/submit/?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F" title="Propeller"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/propeller.png" title="Propeller" alt="Propeller" /></a>
	<a rel="nofollow"  target="_blank" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="Reddit"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" /></a>
	<a rel="nofollow"  target="_blank" href="http://slashdot.org/bookmark.pl?title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F" title="Slashdot"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/slashdot.png" title="Slashdot" alt="Slashdot" /></a>
	<a rel="nofollow"  target="_blank" href="http://socialogs.com/add_story.php?story_url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;story_title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="Socialogs"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/socialogs.png" title="Socialogs" alt="Socialogs" /></a>
	<a rel="nofollow"  target="_blank" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;title=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function" title="StumbleUpon"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/stumbleupon.png" title="StumbleUpon" alt="StumbleUpon" /></a>
	<a rel="nofollow"  target="_blank" href="http://technorati.com/faves?add=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F" title="Technorati"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" /></a>
	<a rel="nofollow"  target="_blank" href="http://twitter.com/home?status=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function%20-%20http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F" title="Twitter"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" /></a>
	<a rel="nofollow"  target="_blank" href="http://buzz.yahoo.com/submit/?submitUrl=http%3A%2F%2Fwww.comteken.com%2Fdatabase-programming%2Fextending-linq-specifying-a-property-in-the-distinct-function%2F&amp;submitHeadline=Extending%20LINQ%20-%20Specifying%20a%20Property%20in%20the%20Distinct%20Function&amp;submitSummary=The%20%22Distinct%22%20function%20in%20LINQ%20is%20definitely%20one%20of%20the%20top%2010%20most%20used%20functions%2C%20but%20it%27s%20probably%20one%20of%20the%20top%2010%20most%20incomplete%20as%20well.%20This%20article%20will%20show%20how%20to%20extend%20the%20IEnumerable%20interface%20to%20allow%20a%20very%20easy%20way%20to%20specify%20what%20&amp;submitCategory=science&amp;submitAssetType=text" title="Yahoo! Buzz"><img src="http://www.comteken.com/wp-content/plugins/sociable/images/yahoobuzz.png" title="Yahoo! Buzz" alt="Yahoo! Buzz" /></a>


<br/><br/>]]></content:encoded>
			<wfw:commentRss>http://www.comteken.com/database-programming/extending-linq-specifying-a-property-in-the-distinct-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

