Domanda

L'anno scorso, Scott Guthrie indicato “Effettivamente si può ignorare il raw SQL, LINQ to SQL utilizza se si vuole il controllo assoluto su SQL eseguito”, ma non riesco a trovare la documentazione che descrive un metodo di estensibilità.

Come posso modificare I dati della seguente query LINQ to SQL:

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

Che si traduce in TSQL seguente:

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

Che sarebbe risultato in TSQL seguente:

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))

Utilizzo:

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

E

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

Utilizzare un certo tipo di LINQ to SQL estensibilità, altro che questo.Tutte le idee?

È stato utile?

Soluzione

La capacità di cambiare il provider sottostante e, quindi, modificare l'SQL non ha fatto il taglio finale in LINQ to SQL.

Altri suggerimenti

Matt Warren blog ha tutto il necessario per che:

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

Si desidera tradurre un'espressione albero in SQL...È necessario implementare la propria IQueryProvider

IQueryProvider Di Riferimento
Come

MSDN Come

DataContext x = new DataContext;

//Qualcosa di simile a questo, forse?

var a = x.Dove().con (), ecc...

ti consente di avere un controllo più preciso dell'sql.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top