Toolroom Tech Blog

Devlopers Digest

T-SQL statement for Entity Framework query

A very useful snippet that creates the T-SQL query for a linq statement.


This very useful snippet below creates the T-SQL query from a linq statement.

/// <summary>
/// Returns the T-SQL string for a given query
/// </summary>
/// <param name="queryable">Source ObjectQuery object to get the SQL for</param>
/// <returns>T-SQL statement for the input query object</returns>
public static string GetQuery(this IQueryable queryable)
	if(queryable == null || !(queryable is ObjectQuery))
		throw new ArgumentException("Argument is null or no ObjectQuery.", "queryable");
	return ((ObjectQuery)queryable).ToTraceString();

Pingbacks and trackbacks (1)+