Jacob
Jacob

Reputation: 7821

Get query syntax for a LINQ query

I have a linq query that i'd like to get the query syntax for.

var q = customers.Where(x => x.name == "smith");

Is there something like IQueryable.ToQuerySyntaxString()? that would return something like this:

from cust in customers where cust.name == "smith";

I'm asking because I can construct my query using method syntax, but would like to see the query syntax equivalent to help me learn how to write in the alternate form.

Upvotes: 3

Views: 772

Answers (3)

Pat
Pat

Reputation: 16891

Resharper will often allow you do that. It can suggest converting from for/foreach to LINQ as well as LINQ back to loops (see http://www.jetbrains.com/resharper/whatsnew/whatsnew_60.html#LINQtoLoops for the latter), plus LINQ method chains to/from query syntax.

Upvotes: 0

Stefan Wenig
Stefan Wenig

Reputation: 178

try re-linq from relinq.codeplex.com

new QueryParser ().GetParsedQuery (q.Expression).ToString()

will give you just that.

Upvotes: 0

Aaronaught
Aaronaught

Reputation: 122634

It actually works the other way around. When you use the second syntax (from x in y where w), it actually gets compiled into the first (y.Where(x => w)).

I'm sure you could write something to produce the second version using Expression Trees, but I'm not aware of anything in the framework that will do it automatically for you.

Upvotes: 1

Related Questions