如何将Linq扩展到SQL? [英] How do you extend Linq to SQL?

查看:71
本文介绍了如何将Linq扩展到SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

去年,斯科特·格思里已声明如果您希望对SQL执行绝对控制,则实际上可以覆盖LINQ to SQL所使用的原始SQL",但是我找不到描述可扩展性方法的文档.

Last year, Scott Guthrie stated "You can actually override the raw SQL that LINQ to SQL uses if you want absolute control over the SQL executed", but I can’t find documentation describing an extensibility method.

我想将以下LINQ修改为SQL查询:

I would like to modify the following LINQ to SQL query:

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

这将导致以下TSQL:

Which results in the following 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进行SQL扩展,而不是

Using some type of LINQ to SQL extensibility, other than this one. Any ideas?

推荐答案

更改底层提供程序并由此修改SQL的能力并未在LINQ to SQL中产生最终的影响.

The ability to change the underlying provider and thus modify the SQL did not make the final cut in LINQ to SQL.

这篇关于如何将Linq扩展到SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆