문제
지난해,Scott Guthrie 밝혔 "할 수 있습니다 실제로 재정의 원 SQL 는 LINQ to SQL 사용하려면 절대적인 통제에 SQL 실행",그러나 나는 찾을 수 없는 문서를 설명하는 확장성 방법입니다.
내가 좋아하는 다음을 수정 LINQ SQL 쿼리:
using (NorthwindContext northwind = new NorthwindContext ()) { var q = from row in northwind.Customers let orderCount = row.Orders.Count () select new { row.ContactName, orderCount }; }
는 다음과 같은 TSQL:
SELECT [t0].[ContactName], ( SELECT COUNT(*) FROM [dbo].[Orders] AS [t1] WHERE [t1].[CustomerID] = [t0].[CustomerID] ) AS [orderCount] FROM [dbo].[Customers] AS [t0]
하기:
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 }; }
는 것 다음과 같은 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))
를 사용:
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; }
고
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; } }
를 사용하여 어떤 종류의 LINQ to SQL 확장,보 이.어떤 아이디어가?
해결책
을 변경하는 기능 기본 제공자고 이렇게 수정할 수 SQL 하지 않았다 최종 LINQ to SQL.
다른 팁
매트 워렌의 블로그에 필요한 모든 것을 갖추고 있는:
매핑되는 x=새로운 매핑되는;
//이 같은 것이라도 있나?
var a=x.어디에().로()...etc.
자 당신은 훨씬 더 정밀한 제어 sql.
제휴하지 않습니다 StackOverflow