The ObjectQuery class has a ToTraceString () function. However, most of the queries you write in LINQ are created as IQueryable, so you must first classify them with ObjectQuery in order to use it.
or, if you define this extension method, you can use it with IQ
public static string ToTraceString<T>(this IQueryable<T> expression) { ObjectQuery<T> objectQuery = expression as ObjectQuery<T>; if (objectQuery != null) { return objectQuery.ToTraceString(); } return ""; }
...
//then you could use it like this IQueryable<Record> records = db.Record.Where(r=>r.Value > x); string generatedQuery = record.ToTraceString();
source share