Note that there are some explanatory texts on larger screens.

plurals
  1. POLearning about LINQ
    text
    copied!<h2>Overview</h2> <p>One of the things I've asked a lot about on this site is <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx" rel="noreferrer">LINQ</a>. The questions I've asked have been wide and varied and often don't have much context behind them. So in an attempt to consolidate the knowledge I've acquired on Linq I'm posting this question with a view to maintaining and updating it with additional information as I continue to learn about LINQ. </p> <p>I also hope that it will prove to be a useful resource for other people wanting to learn about LINQ. </p> <h2>What is LINQ?</h2> <p>From <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx" rel="noreferrer">MSDN</a>:</p> <blockquote> <p>The LINQ Project is a codename for a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.</p> </blockquote> <p>What this means is that LINQ provides a standard way to query a variety of datasources using a common syntax. </p> <h2>What flavours of LINQ are there?</h2> <p>Currently there are a few different LINQ providers provided by Microsoft:</p> <ul> <li><a href="http://msdn.microsoft.com/en-us/library/bb397919.aspx" rel="noreferrer">Linq to Objects</a> which allows you to execute queries on any IEnumerable object. </li> <li><a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx" rel="noreferrer">Linq to SQL</a> which allows you to execute queries against a database in an object oriented manner. </li> <li><a href="http://msdn.microsoft.com/en-us/library/bb387098.aspx" rel="noreferrer">Linq to XML</a> which allows you to query, load, validate, serialize and manipulate XML documents.</li> <li><a href="http://msdn.microsoft.com/en-us/library/bb386964.aspx" rel="noreferrer">Linq to Entities</a> as suggested by <a href="https://stackoverflow.com/questions/16322/all-about-linq#33588">Andrei</a></li> <li><a href="http://msdn.microsoft.com/en-us/library/bb399399.aspx" rel="noreferrer">Linq to Dataset</a></li> </ul> <p>There are quite a few others, many of which are listed <a href="http://blogs.microsoft.co.il/blogs/vardi/archive/2008/10/09/the-linq-list-projects.aspx" rel="noreferrer">here</a>.</p> <h2>What are the benefits?</h2> <ul> <li>Standardized way to query multiple datasources</li> <li>Compile time safety of queries</li> <li>Optimized way to perform set based operations on in memory objects</li> <li>Ability to debug queries</li> </ul> <h2>So what can I do with LINQ?</h2> <p><a href="https://stackoverflow.com/users/489/ch00k">Chook</a> provides a way to <a href="https://stackoverflow.com/questions/4432/csv-string-handling#4441">output CSV files</a><br> <a href="https://stackoverflow.com/users/1/jeff-atwood">Jeff</a> shows how to <a href="https://stackoverflow.com/questions/9673/remove-duplicates-from-array#9685">remove duplicates from an array</a><br> Bob gets a <a href="https://stackoverflow.com/questions/59/how-do-i-get-a-distinct-ordered-list-of-names-from-a-datatable-using-linq#62">distinct ordered list from a datatable</a><br> <a href="https://stackoverflow.com/users/1659/marxidad">Marxidad</a> shows how to <a href="https://stackoverflow.com/questions/15486/sorting-an-ilist-in-c#15495">sort an array</a><br> Dana gets help implementing a <a href="https://stackoverflow.com/questions/185072/learning-linq-quicksort">Quick Sort Using Linq</a> </p> <h2>Where to start?</h2> <p><strong>A summary of links from <a href="https://stackoverflow.com/questions/8050/beginners-guide-to-linq">GateKiller's question</a> are below</strong>:<br> Scott Guthrie provides an <a href="http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx" rel="noreferrer">intro to Linq on his blog</a><br> An overview of <a href="http://msdn.microsoft.com/en-us/library/bb308959.aspx" rel="noreferrer">LINQ on MSDN</a> </p> <p><strong><a href="https://stackoverflow.com/users/1758/ChrisAnnODell">ChrisAnnODell</a> suggests checking out:</strong> </p> <ul> <li><a href="http://www.hookedonlinq.com/MainPage.ashx" rel="noreferrer">Hooked on Linq</a> </li> <li><a href="http://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b" rel="noreferrer">101 Linq Samples</a></li> <li><a href="http://www.linqpad.net/" rel="noreferrer">LinqPad</a> </li> </ul> <h2>What do I need to use LINQ?</h2> <p>Linq is currently available in VB.Net 9.0 and C# 3.0 so you'll need Visual Studio 2008 or greater to get the full benefits. (You could always write your code in notepad and compile using MSBuild)</p> <p>There is also a tool called <a href="https://stackoverflow.com/questions/7652/querying-like-linq-when-you-dont-have-linq#7710">LinqBridge</a> which will allow you to run Linq like queries in C# 2.0. </p> <h2>Tips and tricks using LINQ</h2> <p><a href="https://stackoverflow.com/questions/28858/coolest-c-linqlambdas-trick-youve-ever-pulled">This question</a> has some tricky ways to use LINQ</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload