El año pasado, Scott Guthrie stated "En realidad 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.¿Cómo extiendes Linq a SQL?
me gustaría modificar la siguiente LINQ a SQL consulta:
using (NorthwindContext northwind = new NorthwindContext()) { var q = from row in northwind.Customers let orderCount = row.Orders.Count() select new { row.ContactName, orderCount }; }
que se traduce en la siguiente TSQL:
SELECT [t0].[ContactName], ( SELECT COUNT(*) FROM [dbo].[Orders] AS [t1] WHERE [t1].[CustomerID] = [t0].[CustomerID] ) AS [orderCount] FROM [dbo].[Customers] AS [t0]
Para:
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 }; }
Qué haría da como resultado 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; } }
El uso de algún tipo de LINQ a SQL extensibilidad, aparte de this one. ¿Algunas ideas?
+1 Cualquier posibilidad de que tenga un fuente para esto? –
Estaba en el equipo de desarrollo de LINQ to SQL :) Si desea modificar algunas operaciones específicas, puede utilizar la técnica que se describe aquí http://damieng.com/blog/2009/04/12/linq-to-sql-tips -y-trucos-2, pero no existe un mecanismo de modelo/proveedor de extensibilidad que sea compatible públicamente. – DamienG