Saturday, August 30, 2008 1:29 AM bart

C# 3.0 Query Expression Translation Cheat Sheet

As a reference for some planned and unplanned future posts, I wanted to share out my “cheat sheet” for the C# 3.0 translation carried out for query expressions. Obviously it’s based on the C# 3.0 Language Specification more specially section 7.15.2. A few remarks that deserve more attention when reading the specification:

  • Words in bold are contextual keywords; these are not reserved keywords as the language doesn’t introduce new reserved keywords to ensure forward compatibility for programs written in previous language versions (e.g. “from” could be used as a variable name).
  • Method names indicated in red are the query operators known by the compiler as compilation targets for the corresponding query syntax contextual keywords. Where these methods come from is purely a question answered by method resolution rules, obviously including extension methods from C# 3.0 on.
  • The ‘*’ in the translations below stands for the transparent identifier, more of an implementation aspect of the compiler to “carry forward” the range variables captured in the anonymous type produced by a query operation (e.g. in SelectMany). This illustrates that anonymous types (as well as lambda BLOCKED EXPRESSION are essential pieces of glue to make LINQ work (although alternatives based on a hypothetical “tuple type” runtime feature would work too).
  • For the orderby operator, corresponding flavors with descending order have been omitted. Essentially all of the ordering clauses can optionally have the descending keyword, resulting in an OrderByDescending or ThenByDescending call in the corresponding translation.
  • All the rules are mutually recursive: starting with a query expression, the translation will gradually “compile away” certain parts of the query expression syntax into method call driven equivalent expressions. Notice all rules without an ellipsis … on the left-hand side don’t have any query expression keywords left on the right-hand side – these are the terminal “closed” expressions that are entirely expressible in C# 3.0 \ { LINQ }. It’s like peeling off the LINQ layer from the C# 3.0 onion.
  • The first two rules for Select seem redundant, i.e. when substituting v for x in the second rule one gets the first rule. The reason they are spelled out each individually is because the first rule covers degenerate expressions (covered in another post of mine) and rules should be evaluated from top to bottom. The Select ( x => x ) is kept based on the conditions spelled out in section 7.15.2.3.

So here’s my cheat sheet:

image

Feel free to redistribute it in any form but I’d appreciate at least a reference to the official C# 3.0 Language Specification with it as that’s the ultimate resource for the detailed language specifics. A pointer to my blog would be much appreciated as well of course :-).

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: ,

Comments

# C# 3.0 Query Expression Translation Cheat Sheet

Saturday, August 30, 2008 2:31 AM by C# 3.0 Query Expression Translation Cheat Sheet

Pingback from  C# 3.0 Query Expression Translation Cheat Sheet

# C# 3.0 Query Expression Translation Cheat Sheet

Saturday, August 30, 2008 2:42 PM by K#

C# 3.0 Query Expression Translation Cheat Sheet

# Dew Drop - August 30, 2008 | Alvin Ashcraft's Morning Dew

Saturday, August 30, 2008 4:18 PM by Dew Drop - August 30, 2008 | Alvin Ashcraft's Morning Dew

Pingback from  Dew Drop - August 30, 2008 | Alvin Ashcraft's Morning Dew

# Link Listing - August 30, 2008

Saturday, August 30, 2008 10:39 PM by Christopher Steen

Link Listing - August 30, 2008

# Reflective Perspective - Chris Alcock » The Morning Brew #169

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

# WMOC#18 - Microsoft Solver Foundation released - Service Endpoint

Pingback from  WMOC#18 - Microsoft Solver Foundation released - Service Endpoint

# re: C# 3.0 Query Expression Translation Cheat Sheet

Friday, September 05, 2008 1:40 AM by Andrey

Usefull, I'll put it on a wall

# C# 3.0 Query Expression Translation Cheat Sheet

Wednesday, September 17, 2008 8:26 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# The World According to LINQ | MegaSoul

Sunday, September 04, 2011 7:13 AM by The World According to LINQ | MegaSoul

Pingback from  The World According to LINQ | MegaSoul

# Daily Dump 20090820 pt.2 | Blog .Ronald

Wednesday, April 24, 2013 4:23 AM by Daily Dump 20090820 pt.2 | Blog .Ronald

Pingback from  Daily Dump 20090820 pt.2 | Blog .Ronald