Reputation: 83376
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();
Upvotes: 3
Views: 904
Reputation: 96557
For LINQ to SQL you can use the DataContext.Log property, or the DataContext.GetCommand method to get the generated SQL:
var query = dc.Persons.Take(1);
string generatedSql = dc.GetCommand(query).CommandText;
This example returns the following SQL from the AdventureWorks database:
SELECT TOP (1) [t0].[BusinessEntityID], [t0].[PersonType], [t0].[NameStyle], [t0].[Title], [t0].[FirstName], [t0].[MiddleName], [t0].[LastName], [t0].[Suffix], [t0].[EmailPromotion], [t0].[AdditionalContactInfo], [t0].[Demographics], [t0].[rowguid] AS [Rowguid], [t0].[ModifiedDate] FROM [Person].[Person] AS [t0]
Upvotes: 3
Reputation: 888223
ObjectQuery
is an Entity Framework type.
You can't use it with LINQ to SQL.
Upvotes: 2