QueryUtilsExtensions.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Transactions;
  8. namespace EasyDevCore.Database.EntityFrameworkCore
  9. {
  10. /// <summary>
  11. ///
  12. /// </summary>
  13. public static class QueryUtilsExtensions
  14. {
  15. /// <summary>
  16. /// Executes the with nolock.
  17. /// </summary>
  18. /// <typeparam name="T"></typeparam>
  19. /// <typeparam name="R"></typeparam>
  20. /// <param name="query">The query.</param>
  21. /// <param name="command">The command.</param>
  22. /// <returns></returns>
  23. public static R ExecWithNolock<T, R>(this IQueryable<T> query, Func<IQueryable<T>, R> command)
  24. {
  25. using(var tran = CreateNolockScope(null))
  26. {
  27. var result = command(query);
  28. tran.Complete();
  29. return result;
  30. }
  31. }
  32. /// <summary>
  33. /// Executes the with nolock asynchronous.
  34. /// </summary>
  35. /// <typeparam name="T"></typeparam>
  36. /// <typeparam name="R"></typeparam>
  37. /// <param name="query">The query.</param>
  38. /// <param name="command">The command.</param>
  39. /// <returns></returns>
  40. public static async Task<R> ExecWithNolockAsync<T, R>(this IQueryable<T> query, Func<IQueryable<T>, Task<R>> command)
  41. {
  42. using (var tran = CreateNolockScopeAsync(null))
  43. {
  44. var result = await command(query);
  45. tran.Complete();
  46. return result;
  47. }
  48. }
  49. /// <summary>
  50. /// Creates the nolock scope.
  51. /// </summary>
  52. /// <param name="context">The context.</param>
  53. /// <returns></returns>
  54. public static TransactionScope CreateNolockScope(this EasyDbContext context)
  55. {
  56. return new TransactionScope(TransactionScopeOption.Required,
  57. new TransactionOptions()
  58. {
  59. IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
  60. });
  61. }
  62. /// <summary>
  63. /// Creates the nolock scope asynchronous.
  64. /// </summary>
  65. /// <param name="context">The context.</param>
  66. /// <returns></returns>
  67. public static TransactionScope CreateNolockScopeAsync(this EasyDbContext context)
  68. {
  69. return new TransactionScope(TransactionScopeOption.Required,
  70. new TransactionOptions()
  71. {
  72. IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
  73. },
  74. TransactionScopeAsyncFlowOption.Enabled);
  75. }
  76. /// <summary>
  77. /// Firsts the or default with nolock.
  78. /// </summary>
  79. /// <typeparam name="T"></typeparam>
  80. /// <param name="query">The query.</param>
  81. /// <returns></returns>
  82. public static T FirstOrDefaultWithNolock<T>(this IQueryable<T> query)
  83. {
  84. return query.ExecWithNolock(q => q.FirstOrDefault());
  85. }
  86. /// <summary>
  87. /// Firsts the or default with nolock asynchronous.
  88. /// </summary>
  89. /// <typeparam name="T"></typeparam>
  90. /// <param name="query">The query.</param>
  91. /// <returns></returns>
  92. public static async Task<T> FirstOrDefaultWithNolockAsync<T>(this IQueryable<T> query)
  93. {
  94. return await query.ExecWithNolockAsync(q => q.FirstOrDefaultAsync());
  95. }
  96. /// <summary>
  97. /// Firsts the with nolock.
  98. /// </summary>
  99. /// <typeparam name="T"></typeparam>
  100. /// <param name="query">The query.</param>
  101. /// <returns></returns>
  102. public static T FirstWithNolock<T>(this IQueryable<T> query)
  103. {
  104. return query.ExecWithNolock(q => q.First());
  105. }
  106. /// <summary>
  107. /// Firsts the with nolock asynchronous.
  108. /// </summary>
  109. /// <typeparam name="T"></typeparam>
  110. /// <param name="query">The query.</param>
  111. /// <returns></returns>
  112. public static async Task<T> FirstWithNolockAsync<T>(this IQueryable<T> query)
  113. {
  114. return await query.ExecWithNolockAsync(q => q.FirstOrDefaultAsync());
  115. }
  116. /// <summary>
  117. /// Converts to listwithnolock.
  118. /// </summary>
  119. /// <typeparam name="T"></typeparam>
  120. /// <param name="query">The query.</param>
  121. /// <returns></returns>
  122. public static List<T> ToListWithNolock<T>(this IQueryable<T> query)
  123. {
  124. return query.ExecWithNolock(q => q.ToList());
  125. }
  126. /// <summary>
  127. /// Converts to listwithnolockasync.
  128. /// </summary>
  129. /// <typeparam name="T"></typeparam>
  130. /// <param name="query">The query.</param>
  131. /// <returns></returns>
  132. public static async Task<List<T>> ToListWithNolockAsync<T>(this IQueryable<T> query)
  133. {
  134. return await query.ExecWithNolockAsync(q => q.ToListAsync());
  135. }
  136. /// <summary>
  137. /// Converts to arraywithnolock.
  138. /// </summary>
  139. /// <typeparam name="T"></typeparam>
  140. /// <param name="query">The query.</param>
  141. /// <returns></returns>
  142. public static T[] ToArrayWithNolock<T>(this IQueryable<T> query)
  143. {
  144. return query.ExecWithNolock(q => q.ToArray());
  145. }
  146. /// <summary>
  147. /// Converts to arraywithnolockasync.
  148. /// </summary>
  149. /// <typeparam name="T"></typeparam>
  150. /// <param name="query">The query.</param>
  151. /// <returns></returns>
  152. public static async Task<T[]> ToArrayWithNolockAsync<T>(this IQueryable<T> query)
  153. {
  154. return await query.ExecWithNolockAsync(q => q.ToArrayAsync());
  155. }
  156. /// <summary>
  157. /// Counts the with nolock.
  158. /// </summary>
  159. /// <typeparam name="T"></typeparam>
  160. /// <param name="query">The query.</param>
  161. /// <returns></returns>
  162. public static int CountWithNolock<T>(this IQueryable<T> query)
  163. {
  164. return query.ExecWithNolock(q => q.Count());
  165. }
  166. /// <summary>
  167. /// Counts the with nolock asynchronous.
  168. /// </summary>
  169. /// <typeparam name="T"></typeparam>
  170. /// <param name="query">The query.</param>
  171. /// <returns></returns>
  172. public static async Task<int> CountWithNolockAsync<T>(this IQueryable<T> query)
  173. {
  174. return await query.ExecWithNolockAsync(q => q.CountAsync());
  175. }
  176. /// <summary>
  177. /// Longs the count with nolock.
  178. /// </summary>
  179. /// <typeparam name="T"></typeparam>
  180. /// <param name="query">The query.</param>
  181. /// <returns></returns>
  182. public static long LongCountWithNolock<T>(this IQueryable<T> query)
  183. {
  184. return query.ExecWithNolock(q => q.LongCount());
  185. }
  186. /// <summary>
  187. /// Longs the count with nolock asynchronous.
  188. /// </summary>
  189. /// <typeparam name="T"></typeparam>
  190. /// <param name="query">The query.</param>
  191. /// <returns></returns>
  192. public static async Task<long> LongCountWithNolockAsync<T>(this IQueryable<T> query)
  193. {
  194. return await query.ExecWithNolockAsync(q => q.LongCountAsync());
  195. }
  196. }
  197. }