Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:05:59+00:00 2026-05-31T10:05:59+00:00

This is linq to sql, not entity framework I’m trying to call ToStraceString on

  • 0

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();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:06:01+00:00Added an answer on May 31, 2026 at 10:06 am

    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]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

LINQ to Entity Framework 4.0. SQL Server. I'm trying to return a list of
I'm not using LINQ-to-SQL or Entity Framework bits in a web app, and have
Given that directly exposing linq to sql or entity framework classes over web services
This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
How can I write a query similar to this one in LINQ to SQL
I am searching for the Linq-to-SQL equivalent to this query: SELECT [cnt]=COUNT(*), [colB]=SUM(colB), [colC]=SUM(colC),
I had asked this question Adding more attributes to LINQ to SQL entity Now,
When using LINQ-to-SQL or Entity Framework , I can easily generate a class for
I'm making a query with Linq, backed by an Entity Framework datasource. I'm getting
I'm converting from LINQ to SQL to Entity Framework for my ORM and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.