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 : http://aspnetresources.com/blog/linq_sqo__cheat_sheet.aspx . 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:

Download the LINQ CHEAT SHEET

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 01, 2008 2:43 PM by bradvin
Filed under: , ,

Comments

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

Thanks for posting this :)

I'm just getting started with LINQ...

by Rob

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

Nice.

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

This should help!

# re: LINQ Cheat Sheet@ Sunday, November 02, 2008 12:07 AM

This is really helpful. I have some LINQ related blog as well at http://www.technetbuy.com/blog

Good Job!

# re: LINQ Cheat Sheet@ Sunday, November 02, 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

# Reflective Perspective - Chris Alcock » The Morning Brew #214@ Monday, November 03, 2008 2:33 AM

Pingback from  Reflective Perspective - Chris Alcock  » The Morning Brew #214

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

+1 on VB version please

# re: LINQ Cheat Sheet@ Monday, November 03, 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

<=>

orders

.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 03, 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, November 05, 2008 6:18 AM

nice document, I'm sure it will come in handy

by razvantim

# #.think.in infoDose #6 (3rd Nov - 8th Nov)@ Sunday, November 09, 2008 5:34 PM

#.think.in infoDose #6 (3rd Nov - 8th Nov)

# Weekly Web Nuggets #37@ Monday, November 10, 2008 1:08 PM

Pick of the week: Advice For Developers On Starting An Independent Software Vendor (ISV) Business General Tips For Preparing For A Technical Presentation : Scott Hanselman shares how he prepares for technical presentations. LINQ Cheat Sheet : Brad Vincent

# .NET and web development cheat sheets@ Saturday, November 29, 2008 5:26 PM

Pingback from  .NET and web development cheat sheets

# .NET 3.5 ???????????????????????????????????????Cheat Sheets???@ Monday, December 22, 2008 1:18 AM

Pingback from  .NET 3.5 ???????????????????????????????????????Cheat Sheets???

# Tech cheat sheets@ Tuesday, February 03, 2009 1:35 AM

Just wanted to move these links from my bookmarks Agile Agile Work Resources .Net .NET Cheat Sheet The

# Weekly Web Nuggets #37@ Sunday, February 22, 2009 10:36 PM

Pick of the week: Advice For Developers On Starting An Independent Software Vendor (ISV) Business General Tips For Preparing For A Technical Presentation : Scott Hanselman shares how he prepares for technical presentations. LINQ Cheat Sheet : Brad Vincent

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

awesome awesome awesome

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

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

by Brent

Leave a Comment

(required) 
(required) 
(optional)
(required)