如何将Linq扩展到SQL?
发布时间:2020-12-12 08:39:55 所属栏目:MsSql教程 来源:网络整理
导读:去年,Scott Guthrie stated“如果您希望绝对控制执行的SQL,您实际上可以覆盖LINQ to SQL使用的原始SQL”,但是我找不到描述可扩展性方法的文档. 我想修改以下LINQ to SQL查询: using (NorthwindContext northwind = new NorthwindContext ()) { var q = from
|
去年,Scott Guthrie
stated“如果您希望绝对控制执行的SQL,您实际上可以覆盖LINQ to SQL使用的原始SQL”,但是我找不到描述可扩展性方法的文档.
我想修改以下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
};
}
这导致以下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可扩展性,而不是this one.任何想法? 解决方法更改底层提供程序并因此修改SQL的能力并没有使LINQ to SQL的最终裁剪.(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
