BLToolkit : Table-value functions
来源:互联网 发布:在淘宝哪里看手机评估 编辑:程序博客网 时间:2024/05/16 11:56
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.
1 Answers
answered 2011-07-14 14:06:10#1
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)
0 0
- BLToolkit : Table-value functions
- BLToolkit : Mapping a code table
- Oracle Pipelined Table Functions
- The virtual functions table
- Oracle Pipelined Table Functions
- Oracle Pipelined Table Functions
- The virtual functions table
- Table-Valued User-Defined Functions
- Oracle Pipelined Table Functions简介
- Oracle Pipelined Table Functions简介
- BLToolkit 简介
- BLToolkit AzureSql
- BLtoolkit : association
- Hash Functions for Hash Table Lookup
- Hash Functions for Hash Table Lookup
- Oracle Pipelined Table Functions(转)
- Hash Functions for Hash Table Lookup
- CDS Table Functions Implemented by AMDP
- 决策树模型组合之随机森林与GBDT
- java多线程(九)阻塞队列
- Spring Security简介
- Next Permutation
- 原来微信支付可以这么简单!手把手搭建支付demo
- BLToolkit : Table-value functions
- xcode导入swift库出错
- Android studio中log日志工具的用法
- OpenStack常用命令之nova
- 使用maven编译zxing core源码
- hdu5375 Gray code
- hdoj 3466 【01背包】【DP】
- 获取客户端IP地址方法
- Android 在DOS控制台下查看所创建的数据库