开发者

How to translate linq query to a human readable string

How do I translate a linq query (or a expression like below) to a human readable string? Useful for debugging purposes.

Expression<Func<User, bool>> query

Edit

Since it was hard to understand: I want to get a string from the expression. Hence the example declaration of the expression.

Expression<Func<User, bool>> query2 = 
   u => u.FirstName.StartsWith("J") && u.LastName == "Gauffin";

Should print something l开发者_高级运维ike "FirstName startswith 'J' and LastName equals 'Gauffin'";


Tip: if you are querying using linq to sql or entity framework you can use the ToString() method on the IQueryable object to get the query in sql:

query2.ToString() gives something like:

SELECT * FROM Table1 WHERE FirstName LIKE 'J%' AND LastName="Gauffin"


Check out the LINQ Expression Visualizer in the VS samples folder.

Alternatively in LINQ to SQL you can see it using the DataContext.Log

In Entity Framework you cast the LINQ query to an ObjectQuery and call ToTraceString

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜