Overview

One of the things I've asked a lot about on this site is LINQ. 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.

I also hope that it will prove to be a useful resource for other people wanting to learn about LINQ.

What is LINQ?

From MSDN:

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.

What this means is that LINQ provides a standard way to query a variety of datasources using a common syntax.

What flavours of LINQ are there?

Currently there are a few different LINQ providers provided by Microsoft:

  • Linq to Objects which allows you to execute queries on any IEnumerable object.
  • Linq to SQL which allows you to execute queries against a database in an object oriented manner.
  • Linq to XML which allows you to query, load, validate, serialize and manipulate XML documents.
  • Linq to Entities as suggested by Andrei
  • Linq to Dataset

There are quite a few others, many of which are listed here.

What are the benefits?

  • Standardized way to query multiple datasources
  • Compile time safety of queries
  • Optimized way to perform set based operations on in memory objects
  • Ability to debug queries

So what can I do with LINQ?

Chook provides a way to output CSV files
Jeff shows how to remove duplicates from an array
Bob gets a distinct ordered list from a datatable
Marxidad shows how to sort an array
Dana gets help implementing a Quick Sort Using Linq

Where to start?

A summary of links from GateKiller's question are below:
Scott Guthrie provides an intro to Linq on his blog
An overview of LINQ on MSDN

ChrisAnnODell suggests checking out:

  • Hooked on Linq
  • 101 Linq Samples
  • LinqPad

What do I need to use LINQ?

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)

There is also a tool called LinqBridge which will allow you to run Linq like queries in C# 2.0.

Tips and tricks using LINQ

This question has some tricky ways to use LINQ


Solution 1:

LINQ to entities:

  • Video walkthroughs
  • Channel 9 video
  • Entity framework FAQ
  • Entity framework performance

I've got a lot more I tagged on Delicious.com.

Solution 2:

Mention LINQ to Entities since ADO.NET Entity Framework will be an important .NET module.

Solution 3:

A few LINQ Tips:

  • Apply filters before a join to improve query performance
  • Filter LINQ queries using object reference comparison
  • Apply aggregates to empty collections in LINQ to SQL queries
  • Delay loading a property in LINQ to SQL
  • Use table-valued functions with eager loading turned on
  • Put joins in the correct order in a LINQ to Objects query
  • Compose a LINQ query inside a loop

http://www.aspnetpro.com/articles/2009/04/asp200904zh_f/asp200904zh_f.asp

Solution 4:

Get the book Linq in Action it is an easy read for a coding book and really teaches you how to use Linq and the new features of .NET 3.5 some of the cool parts they put in for the language.