Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: SQL Statement used by LINQ

Compare Page Revisions



« Older Revision - Back to Page History - Current Revision


Page Revision: Wed, Aug 24, 2011, 1:49 PM


LINQ-to-SQL

To send it to the Console.

using (NorthwindDataContext context = new NorthwindDataContext())
{          
    context.Log = Console.Out;

    Customer customer = context.Customers.Single<Customer>
                       (c => c.CustomerID.Equals("ALFKI"));
}

To send it to the debugger output window. (DebuggerWriter is a wrapper around System.Diagnostics.Debugger.
using (NorthwindDataContext context = new NorthwindDataContext())
{          
    context.Log = new DebuggerWriter();

    Customer customer = context.Customers.Single<Customer>
                        (c => c.CustomerID.Equals("ALFKI"));
}

Entity Framework

IQueryable<Product> q = from o in . . .

string sql = ((ObjectQuery<Product>)q).ToTraceString();

Consider adding the following to the same namespace as your Entity Model. It will add a DebugSql extension method to every IQueryable in your code, and will output the underlying SQL statement.

public static class Extender
{
    public static void DebugSql(this IQueryable q)
    {
        var o = q as ObjectQuery;
        if (o == null) return;
        Debug.Print("=".PadLeft(100, '='));
        Debug.Print(o.ToTraceString());
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.