Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to Entities: see the resulting query (context.Log = Console.Out)

I just realized if your C# application use LINQ-TO-SQL classes to interface with the database, you can your query like this

        using (DatabaseContext context = new DatabaseContext())
        {
            context.Log = Console.Out;
            var query = from Person p in context.People
                        where person.Name == "john"
                        select p;                                
            Console.WriteLine(query.Name);
        }

What is the equivalent in LINQ-TO-ENTITY (is this another name for ADO.NET?) for
context.Log = Console.Out
Or there is another way to see your actual SQL query to the database?

like image 446
nobody Avatar asked Feb 12 '11 07:02

nobody


1 Answers

I always use SQL Profiler if you have MS SQL Server. What DBMS is this for? LINQ 2 Entities supports multiple DB types.

This also works...

var cust = (from c in context.Customers select c);

string sql = ((ObjectQuery)cust).ToTraceString();

From MSDN forums

like image 194
SliverNinja - MSFT Avatar answered Oct 09 '22 00:10

SliverNinja - MSFT