By lomaxx


2008-08-19 14:50:43 8 Comments

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:

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:

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

9 comments

@BKSpurgeon 2017-01-28 00:34:07

For Linq Practice

If you want some practice on LINQ with exercises and answers, really easy to set up and, in my opinion, awesome:

https://github.com/walkhard/linq-exercises

Download from git, open in Visual Studio. Your job is to make the tests pass.

[disclosure: i learned some linq from it and I contribute to the project so yeah i think it's an awesome, fast and efficient way to learn.]

@refactor 2015-10-07 11:19:26

My 2 cents , Read chapters "11 Query expressions and LINQ to Objects" and "12 LINQ beyond collections" in "C# in Depth" book to understand how LINQ works.

@Matthew Sposato 2009-12-10 21:22:22

IMHO, an overlooked, but important, benefit is the coding efficiency of LINQ, e.g how much can be accomplished with so little code. I personally find the query syntax easy to read and comprehend.

@Alexandra Rusina 2009-11-06 17:56:08

I think, the answer to "What flavors of LINQ are there?" is incomplete. First of all, you can create your own "flavor". Yes, it is an advanced task, but there are a lot of different LINQ implementations now.

Here is the list of existing LINQ providers (plus some more resources on learning LINQ) on Charlie Calvert's blog: Links to LINQ.

And also there is an excellent series of blog posts by Matt Warren on how to create your own LINQ Provider: LINQ: Building an IQueryable provider series

@Chris S 2008-10-12 11:00:03

@Soner Gönül 2019-03-04 11:20:26

Why this delicious.com redirects to an extension name called Dmail on chrome store? o.O You mean del.icio.us instead?

@Konstantin Tarkus 2009-04-03 20:14:24

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

@DOK 2008-11-07 14:46:42

Some caveats about using LINQ to SQL:

Has Microsoft really killed LINQ to SQL?

Is LINQ to SQL DOA?

There's also some controversy about the first version of Entity Framework, including a petition.

@David Basarab 2008-08-30 02:02:43

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.

@Andrei Rînea 2008-08-28 22:52:29

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

Related Questions

Sponsored Content

20 Answered Questions

[SOLVED] LINQ's Distinct() on a particular property

9 Answered Questions

[SOLVED] Group by in LINQ

  • 2011-09-06 19:44:20
  • test123
  • 1134209 View
  • 940 Score
  • 9 Answer
  • Tags:   c# linq group-by

14 Answered Questions

[SOLVED] LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria

  • 2009-11-16 23:59:33
  • p.campbell
  • 254570 View
  • 458 Score
  • 14 Answer
  • Tags:   .net linq linq-to-sql

13 Answered Questions

[SOLVED] When to use .First and when to use .FirstOrDefault with LINQ?

  • 2009-06-21 19:15:29
  • Metro Smurf
  • 511326 View
  • 766 Score
  • 13 Answer
  • Tags:   c# .net linq

21 Answered Questions

[SOLVED] LINQ query on a DataTable

16 Answered Questions

[SOLVED] Entity Framework vs LINQ to SQL

19 Answered Questions

[SOLVED] Dynamic LINQ OrderBy on IEnumerable<T> / IQueryable<T>

34 Answered Questions

[SOLVED] What is the Java equivalent for LINQ?

  • 2009-08-01 18:53:26
  • Ahmed
  • 255840 View
  • 785 Score
  • 34 Answer
  • Tags:   java linq

7 Answered Questions

[SOLVED] Multiple "order by" in LINQ

  • 2008-11-18 13:34:11
  • Sasha
  • 553127 View
  • 1486 Score
  • 7 Answer
  • Tags:   linq sql-order-by

10 Answered Questions

[SOLVED] What is the leading LINQ for JavaScript library?

  • 2010-03-19 14:31:16
  • Tom Tresansky
  • 36549 View
  • 94 Score
  • 10 Answer
  • Tags:   javascript json linq

Sponsored Content