tags:

views:

49

answers:

3

I think it would be useful to have a linq preprocessor where it can "preprocess" your linq expression into regular c# statements like .Select() .Group() etc. This would be useful for learning what goes on behind the scenes in linq, learning linq or debugging a complex linq expression. Does such a tool or facility exist? I couldn't find this in LinqPad.

+1  A: 

Resharper can convert LINQ expressions into method chains.

spender
+1  A: 

You could also try running Linq expressions through Reflector (Specifically, see complaint 2).

R0MANARMY
+3  A: 

To use this feature in LINQPad, run a query and then click the lambda button on the results window. Note that this works only for IQueryable-based queries. This means that for local queries, you must call .AsQueryable():

from n in new[] { "Tom", "Dick", "Harry" }.AsQueryable()
where n.Contains ("a")
select n

The translation from query expressions into fluent syntax is particularly interesting with queries that have multiple generators, joins or the let statement. For instance:

var fullNames = new[] { "Anne Williams", "John Fred Smith", "Sue Green" }.AsQueryable();

IEnumerable<string> query =
  from fullName in fullNames
  from name in fullName.Split()
  orderby fullName, name
  select name + " came from " + fullName;

query.Dump();

This translates to:

System.String[]
   .SelectMany (
      fullName => fullName.Split (new Char[0]), 
      (fullName, name) => 
         new  
         {
            fullName = fullName, 
            name = name
         }
   )
   .OrderBy (temp0 => temp0.fullName)
   .ThenBy (temp0 => temp0.name)
   .Select (temp0 => ((temp0.name + " came from ") + temp0.fullName))
Joe Albahari