博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linq to sql 扩展方法
阅读量:5302 次
发布时间:2019-06-14

本文共 3762 字,大约阅读时间需要 12 分钟。

老赵的博客:

                

1、DataContext扩展方法
public static class DataContextExentions    {        ///         /// 打开连接        ///         ///         private static void OpenConnection(this DataContext dataContext)        {            if (dataContext.Connection.State == ConnectionState.Closed)            {                dataContext.Connection.Open();            }        }        ///         /// 扩展ExecuteQuery方法         ///         /// 
/// /// /// ///
public static List
ExecuteQuery
(this DataContext dataContext, IQueryable query, bool withNoLock) { DbCommand command = dataContext.GetCommand(query, withNoLock); dataContext.OpenConnection(); using (DbDataReader reader = command.ExecuteReader()) { return dataContext.Translate
(reader).ToList(); } } ///
/// 扩展ExecuteQuery方法2 /// ///
///
///
///
///
public static List
ExecuteQuery
(this DataContext dataContext, IQueryable query) { DbCommand command = dataContext.GetCommand(query); dataContext.OpenConnection(); using (DbDataReader reader = command.ExecuteReader()) { return dataContext.Translate
(reader).ToList(); } } ///
/// 扩展GetCommend方法,允许设置WithNoLick /// ///
///
///
///
private static SqlCommand GetCommand(this DataContext dataContext, IQueryable query, bool withNoLock) { SqlCommand command = (SqlCommand)dataContext.GetCommand(query); if (withNoLock) { command.CommandText = AddWithNoLock(command.CommandText); } return command; } ///
/// 将Sql语句修改为with nolock /// ///
///
private static string AddWithNoLock(string cmdText) { IEnumerable
matches = s_withNoLockRegex.Matches(cmdText).Cast
() .OrderByDescending(m => m.Index); foreach (Match m in matches) { int splitIndex = m.Index + m.Value.Length; cmdText = cmdText.Substring(0, splitIndex) + " WITH (NOLOCK)" + cmdText.Substring(splitIndex); } return cmdText; } private static Regex s_withNoLockRegex = new Regex(@"(] AS [td+])", RegexOptions.Compiled); }}
View Code
2、DataContext扩展方法支持分页
/// /// DataContext扩展方法/// public static class DataContextExtends{    ///     /// ExecuteQuery方法扩展,将对象以redader方式转换为实体    ///     /// 
/// /// ///
public static List
ExecuteQuery
(this DataContext dataContext, IQueryable
query) { return ExecuteQuery
(dataContext, query, 1, query.Cast
().Count()); } ///
/// ExecuteQuery方法扩展,代表分页的 /// ///
///
///
///
///
///
public static List
ExecuteQuery
(this DataContext dataContext, IQueryable
query, int pageIndex, int pageSize) { int total = query.Count(); int totalPages = total / pageSize; if (total % pageSize > 0) totalPages++; if (pageIndex > totalPages) { pageIndex = totalPages; } if (pageIndex < 1) { pageIndex = 1; } query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); DbCommand command = dataContext.GetCommand(query); dataContext.OpenConnection(); using (DbDataReader reader = command.ExecuteReader()) { return dataContext.Translate
(reader).ToList(); } } private static void OpenConnection(this DataContext dataContext) { if (dataContext.Connection.State == ConnectionState.Closed) dataContext.Connection.Open(); }}
View Code

 

转载于:https://www.cnblogs.com/51net/p/3994700.html

你可能感兴趣的文章
mysql忘记密码的解决办法
查看>>
全面分析Java的垃圾回收机制2
查看>>
[Code Festival 2017 qual A] C: Palindromic Matrix
查看>>
修改博客园css样式
查看>>
Python3 高阶函数
查看>>
初始面向对象
查看>>
docker一键安装
查看>>
leetcode Letter Combinations of a Phone Number
查看>>
Exercise 34: Accessing Elements Of Lists
查看>>
angular中的代码执行顺序和$scope.$digest();
查看>>
ALS算法 (面试准备)
查看>>
思达BI软件Style Intelligence实例教程—房地产分析
查看>>
Unity 3D 如何修改新建脚本中的 C# 默认创建的 Script 脚本格式
查看>>
Unity 5.4 测试版本新特性---因吹丝停
查看>>
7.5 文件操作
查看>>
DFS-hdu-2821-Pusher
查看>>
Spring事务管理的三种方式
查看>>
MyEclipse中将普通Java项目convert(转化)为Maven项目
查看>>
node js 安装.node-gyp/8.9.4 权限 无法访问
查看>>
Java_正则表达式
查看>>