BLToolkit 中的表值函数

发布于 2024-11-19 18:52:19 字数 145 浏览 4 评论 0原文

是否可以通过 BLToolkit 库来使用 SQL Server 表值函数?

我想在 Linq 查询中使用它,但我在库 wiki 上找不到任何与此相关的内容。

Is it possible to use SQL Server table-value functions by using the BLToolkit library?

I would like to use it within the Linq query, but I couldn't find anything regarding this on the library wiki.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生一个溪 2024-11-26 18:52:19

在数据上下文类中定义函数,如下所示:

[TableFunction(Name="GetParentByID")]
public Table<Parent> GetParentByID(int? id)
{
    return GetTable<Parent>(this, (MethodInfo)MethodBase.GetCurrentMethod(), id);
}

用法:

[Test]
public void Func2()
{
    using (var db = new TestDbManager())
    {
        var q =
            from c in db.Child
            from p in db.GetParentByID(2)
            select p;

        q.ToList();
    }
}

SQL:

SELECT
    [t2].[ParentID],
    [t2].[Value1]
FROM
    [Child] [t1], [GetParentByID](2) [t2]

您也可以在数据上下文之外定义它:

public class Functions
{
    private readonly IDataContext _ctx;

    public Functions(IDataContext ctx)
    {
        _ctx = ctx;
    }

    [TableFunction]
    public Table<Parent> GetParentByID(int? id)
    {
        return _ctx.GetTable<Parent>(this, (MethodInfo)(MethodBase.GetCurrentMethod()), id);
    }

    [TableExpression("{0} {1} WITH (TABLOCK)")]
    public Table<T> WithTabLock<T>()
        where T : class 
    {
        return _ctx.GetTable<T>(this, ((MethodInfo)(MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof(T)));
    }
}

[Test]
public void Func1()
{
    using (var db = new TestDbManager())
    {
        var q =
            from p in new Functions(db).GetParentByID(1)
            select p;

        q.ToList();
    }
}

[Test]
public void WithTabLock()
{
    using (var db = new TestDbManager())
    {
        var q =
            from p in new Functions(db).WithTabLock<Parent>()
            select p;

        q.ToList();
    }
}

SQL:

SELECT
    [p].[ParentID],
    [p].[Value1]
FROM
    [GetParentByID](1) [p]

SELECT
    [p].[ParentID],
    [p].[Value1]
FROM
    [Parent] [p] WITH (TABLOCK)

Define your function in your data context class as the following:

[TableFunction(Name="GetParentByID")]
public Table<Parent> GetParentByID(int? id)
{
    return GetTable<Parent>(this, (MethodInfo)MethodBase.GetCurrentMethod(), id);
}

Usage:

[Test]
public void Func2()
{
    using (var db = new TestDbManager())
    {
        var q =
            from c in db.Child
            from p in db.GetParentByID(2)
            select p;

        q.ToList();
    }
}

SQL:

SELECT
    [t2].[ParentID],
    [t2].[Value1]
FROM
    [Child] [t1], [GetParentByID](2) [t2]

Also you can define it outside of the data context:

public class Functions
{
    private readonly IDataContext _ctx;

    public Functions(IDataContext ctx)
    {
        _ctx = ctx;
    }

    [TableFunction]
    public Table<Parent> GetParentByID(int? id)
    {
        return _ctx.GetTable<Parent>(this, (MethodInfo)(MethodBase.GetCurrentMethod()), id);
    }

    [TableExpression("{0} {1} WITH (TABLOCK)")]
    public Table<T> WithTabLock<T>()
        where T : class 
    {
        return _ctx.GetTable<T>(this, ((MethodInfo)(MethodBase.GetCurrentMethod())).MakeGenericMethod(typeof(T)));
    }
}

[Test]
public void Func1()
{
    using (var db = new TestDbManager())
    {
        var q =
            from p in new Functions(db).GetParentByID(1)
            select p;

        q.ToList();
    }
}

[Test]
public void WithTabLock()
{
    using (var db = new TestDbManager())
    {
        var q =
            from p in new Functions(db).WithTabLock<Parent>()
            select p;

        q.ToList();
    }
}

SQL:

SELECT
    [p].[ParentID],
    [p].[Value1]
FROM
    [GetParentByID](1) [p]

SELECT
    [p].[ParentID],
    [p].[Value1]
FROM
    [Parent] [p] WITH (TABLOCK)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文