BradVin's .Net Blog

Code, snippets, controls, utils, etc. Basically all things .net

LINQ Cheat Sheet

 A while ago when I was learning LINQ I searched the web for a 'cheat sheet'. I needed a single document that covered most of the common scenarios w.r.t LINQ so that I would not need to google every time I was writing LINQ queries. I found one very good one : . But I wanted more (as usual) so I created my own. I was also new to lambda expressions and I wanted to learn the differences between syntax in a LINQ query written in 'query syntax' and 'lambda syntax' so I also included that as part of my cheat sheet. Every query is given in both syntaxes. Here is a screenshot:


Please let me know what you think. Did it help you? Where could it be improved? What would you add?

You can also download the code that I used when creating the document. It also includes basic testing to make sure the queries of both syntaxes returned the same results. Beware as it may contain some bugs ;)

UPDATE (3 Nov 2008) :  I updated the cheat sheet document. Changes made:

  • made the left and right margins smaller
  • adjusted column widths to be equal
  • added new example under 'Ordering' section (thanks to RichardD - see comments)
  • added new example under 'Element Operators' section and updated notes (thanks to RichardD - see comments)


Published Saturday, November 1, 2008 2:43 PM by bradvin
Filed under: , ,


# re: LINQ Cheat Sheet@ Saturday, November 1, 2008 10:47 AM

Thanks for posting this :)

I'm just getting started with LINQ...

by Rob

# re: LINQ Cheat Sheet@ Saturday, November 1, 2008 12:26 PM


6 months later i still find myself going to google for certain things.

This should help!

# re: LINQ Cheat Sheet@ Sunday, November 2, 2008 6:49 AM

Great work.

I was also looking for this resource, and found your cheat sheet very useful.

Please make a VB version of the cheat sheet.

Thanks again.

by Moiz

# re: LINQ Cheat Sheet@ Monday, November 3, 2008 6:23 AM

+1 on VB version please

# re: LINQ Cheat Sheet@ Monday, November 3, 2008 2:36 PM

Very nice. Just a couple of comments:

"Ordering" - you have an example for multiple orderby clauses, but not for a single orderby clause with multiple expressions:

from o in orders

orderby o.CustomerID, o.Cost descending

select o



.OrderBy(o => o.CustomerID)

.ThenByDescending(o => o.Cost)

"SingleOrDefault, ... all return NULL if source sequence is empty"

Actually, they return default(T) if source is empty. This will be NULL if T is a reference type or nullable value type; non-nullable value types default to 0 / false, etc.

# re: LINQ Cheat Sheet@ Monday, November 3, 2008 4:01 PM

thanks RichardD, you are most definately correct. I have updated the doc and added 2 new examples.

by bradvin

# re: LINQ Cheat Sheet@ Wednesday, February 25, 2009 11:44 AM

awesome awesome awesome

# re: LINQ Cheat Sheet@ Friday, May 1, 2009 3:56 PM

I'm glad there are great people out there like you who do this stuff for me.  Thanks!

by Brent

# re: LINQ Cheat Sheet@ Monday, September 7, 2009 2:22 PM

The cheat sheet is missing SelectMany().

There are 2 types of joins: inner and outer. in linq one is achieved through Join() another through SelectMany().

by Boris