Pregunta

El año pasado, Scott Guthrie fijado "De hecho, puede anular el SQL sin formato que utiliza LINQ to SQL si desea un control absoluto sobre el SQL ejecutado", pero no puedo encontrar documentación que describa un método de extensibilidad.

Me gustaría modificar la siguiente consulta de LINQ a SQL:

using (NorthwindContext northwind = new NorthwindContext ()) {
    var q = from row in northwind.Customers
            let orderCount = row.Orders.Count ()
            select new {
                row.ContactName,
                orderCount
            };
}

Lo que da como resultado el siguiente TSQL:

SELECT [t0].[ContactName], (
    SELECT COUNT(*)
    FROM [dbo].[Orders] AS [t1]
    WHERE [t1].[CustomerID] = [t0].[CustomerID]
    ) AS [orderCount]
FROM [dbo].[Customers] AS [t0]

A:

using (NorthwindContext northwind = new NorthwindContext ()) {
    var q = from row in northwind.Customers.With (
                        TableHint.NoLock, TableHint.Index (0))
            let orderCount = row.Orders.With (
                        TableHint.HoldLock).Count ()
            select new {
                row.ContactName,
                orderCount
            };
}

Cual haría resulta en el siguiente TSQL:

SELECT [t0].[ContactName], (
    SELECT COUNT(*)
    FROM [dbo].[Orders] AS [t1] WITH (HOLDLOCK)
    WHERE [t1].[CustomerID] = [t0].[CustomerID]
    ) AS [orderCount]
FROM [dbo].[Customers] AS [t0] WITH (NOLOCK, INDEX(0))

Usando:

public static Table<TEntity> With<TEntity> (
    this Table<TEntity> table,
    params TableHint[] args) where TEntity : class {

    //TODO: implement
    return table;
}
public static EntitySet<TEntity> With<TEntity> (
    this EntitySet<TEntity> entitySet,
    params TableHint[] args) where TEntity : class {

    //TODO: implement
    return entitySet;
}

Y

public class TableHint {
    //TODO: implement
    public static TableHint NoLock;
    public static TableHint HoldLock;
    public static TableHint Index (int id) {
        return null;
    }
    public static TableHint Index (string name) {
        return null;
    }
}

Usar algún tipo de extensibilidad de LINQ a SQL, que no sea Éste.¿Algunas ideas?

¿Fue útil?

Solución

La capacidad de cambiar el proveedor subyacente y así modificar el SQL no llegó al corte final en LINQ to SQL.

Otros consejos

El blog de Matt Warren tiene todo lo que necesitas para eso:

http://blogs.msdn.com/mattwar/

Quiere traducir un árbol de expresión a SQL...Necesita implementar su propio IQueryProvider

Referencia de IQueryProvider
Cómo

MSDN Cómo

Contexto de datos x = nuevo contexto de datos;

// ¿Algo como esto quizás?

var a = x.Dónde().con()...etc.

Tengamos un control mucho más preciso sobre el SQL.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top