2011-07-13 17 views
1

BLToolkitライブラリを使用してSQL Serverテーブル値関数を使用することはできますか?BLToolkitのテーブル値関数

Linqクエリで使用したいと思いますが、これについてはライブラリwikiで何も見つかりませんでした。

答えて

3

次のようにあなたのデータコンテキストクラスのあなたの関数を定義します。

[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(); 
    } 
} 
関連する問題