去年,Scott Guthrie stated“如果您想绝对控制执行的SQL,则实际上可以覆盖LINQ to SQL使用的原始SQL,但我找不到描述可扩展性方法的文档。
我想将以下LINQ修改为SQL查询:
使用(NorthwindContext northwind = new NorthwindContext()){var q =来自罗斯文(Northwind)中的行。让orderCount = row.Orders.Count()选择新的{row.ContactName,orderCount};}
将导致以下TSQL:
SELECT [t0]。[ContactName],(选择计数(*)从[dbo]。[订单] AS [t1]在[t1]。[CustomerID] = [t0]。[CustomerID])AS [orderCount]来自[dbo]。[客户] AS [t0]
收件人:
使用(NorthwindContext northwind = new NorthwindContext()){var q = from northwind.Customers.With(TableHint.NoLock,TableHint.Index(0))让orderCount = row.Orders.With(TableHint.HoldLock).Count()选择新的{row.ContactName,orderCount};}
哪个将导致以下TSQL:
SELECT [t0]。[ContactName],(选择计数(*)FROM [dbo]。[Orders] AS [t1] WITH(HOLDLOCK)在[t1]。[CustomerID] = [t0]。[CustomerID])AS [orderCount]从[dbo]。[客户]为[t0],带(NOLOCK,INDEX(0))
使用中:
公共静态表与 (此Table 表,参数TableHint [] args),其中TEntity:类{// TODO:实作退货表;}公共静态EntitySet With (此EntitySet 实体集,参数TableHint [] args),其中TEntity:类{// TODO:实作返回entitySet;}
和
公共类TableHint {// TODO:实作公共静态TableHint NoLock;公共静态TableHint HoldLock;公共静态TableHint索引(int id){返回null;}公共静态TableHint索引(字符串名称){返回null;}}
除this one以外,对SQL可扩展性使用某种类型的LINQ。有什么想法吗?
更改基础提供程序并因此修改SQL的能力并未使LINQ最终切入SQL。
Matt Warren的博客提供了您所需的一切:
您想将表达式树转换为SQL ...您需要实现自己的IQueryProvider
DataContext x = new DataContext