This is linq to sql, not entity framework
I’m trying to call ToStraceString on a query that returns an anonymous type, but my cast to ObjectQuery is failing.
Is there some of other way to cast this query to ObjectQuery, or is there some other way to grab the generated T-SQL short of starting a SQL Server trace?
var junk = db.SiteProducts
.Where(p => p.SiteProductId == SiteProductId)
.Select(p => new
{
p.SiteProductId,
ItemSku = p.ItemMaster != null ? p.ItemMaster.ItemSku : "",
p.AdminDisplayName,
p.CurrentInventory,
p.IsFreeGift,
p.SiteDivisionId,
p.PrimaryParentSiteCategoryId,
p.UsesVariantAttributes,
UsesOmsPz = p.ItemMaster != null ? p.ItemMaster.OmsPzTemplateId.HasValue : false,
p.HasDetailPage,
div = p.SiteDivision.AdminDisplayName,
domain = p.Site.PrimaryDomain
});
string str = ((System.Data.Objects.ObjectQuery)junk).ToTraceString();
For LINQ to SQL you can use the DataContext.Log property, or the DataContext.GetCommand method to get the generated SQL:
This example returns the following SQL from the AdventureWorks database: