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

SQL Statement used by LINQ

RSS
Modified on Fri, Nov 08, 2013, 5:25 PM by Administrator Categorized as LINQ, ┬ĚNet Framework

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 4.0

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());
    }
}

Entity Framework 4.5

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

string sql = q.ToString();

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