Different answer for different problem.
You can't call ToTraceString()
on this:
var product = _context.Products.Where( p => p.Category == "Windows" )
.SingleOrDefault();
You can do this:
var q = _context.Products.Where( p => p.Category == "Windows" )
var ts = ((ObjectQuery)q).ToTraceString();
var product = q.SingleOrDefault();
... but it's not 100% accurate. The MSSQL EF provider will use a TOP 2
for Single
which this will miss.
You can come close with this:
var q = _context.Products.Where( p => p.Category == "Windows" )
var ts = ((ObjectQuery)q.Take(2)).ToTraceString();
var product = q.SingleOrDefault();
...which should get you the right SQL but requires knowledge of the implementation.
Original question misrepresented the problem. My original answer was:
var ts = (product as ObjectQuery).ToTraceString();