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 cus开发者_如何学JAVAtomers 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.
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.
try re-linq from relinq.codeplex.com
new QueryParser ().GetParsedQuery (q.Expression).ToString()
will give you just that.
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.
精彩评论