123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Transactions;
- namespace EasyDevCore.Database.EntityFrameworkCore
- {
- /// <summary>
- ///
- /// </summary>
- public static class QueryUtilsExtensions
- {
- /// <summary>
- /// Executes the with nolock.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <typeparam name="R"></typeparam>
- /// <param name="query">The query.</param>
- /// <param name="command">The command.</param>
- /// <returns></returns>
- public static R ExecWithNolock<T, R>(this IQueryable<T> query, Func<IQueryable<T>, R> command)
- {
- using(var tran = CreateNolockScope(null))
- {
- var result = command(query);
- tran.Complete();
- return result;
- }
- }
- /// <summary>
- /// Executes the with nolock asynchronous.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <typeparam name="R"></typeparam>
- /// <param name="query">The query.</param>
- /// <param name="command">The command.</param>
- /// <returns></returns>
- public static async Task<R> ExecWithNolockAsync<T, R>(this IQueryable<T> query, Func<IQueryable<T>, Task<R>> command)
- {
- using (var tran = CreateNolockScopeAsync(null))
- {
- var result = await command(query);
- tran.Complete();
- return result;
- }
- }
- /// <summary>
- /// Creates the nolock scope.
- /// </summary>
- /// <param name="context">The context.</param>
- /// <returns></returns>
- public static TransactionScope CreateNolockScope(this EasyDbContext context)
- {
- return new TransactionScope(TransactionScopeOption.Required,
- new TransactionOptions()
- {
- IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
- });
- }
- /// <summary>
- /// Creates the nolock scope asynchronous.
- /// </summary>
- /// <param name="context">The context.</param>
- /// <returns></returns>
- public static TransactionScope CreateNolockScopeAsync(this EasyDbContext context)
- {
- return new TransactionScope(TransactionScopeOption.Required,
- new TransactionOptions()
- {
- IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
- },
- TransactionScopeAsyncFlowOption.Enabled);
- }
- /// <summary>
- /// Firsts the or default with nolock.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static T FirstOrDefaultWithNolock<T>(this IQueryable<T> query)
- {
- return query.ExecWithNolock(q => q.FirstOrDefault());
- }
- /// <summary>
- /// Firsts the or default with nolock asynchronous.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static async Task<T> FirstOrDefaultWithNolockAsync<T>(this IQueryable<T> query)
- {
- return await query.ExecWithNolockAsync(q => q.FirstOrDefaultAsync());
- }
- /// <summary>
- /// Firsts the with nolock.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static T FirstWithNolock<T>(this IQueryable<T> query)
- {
- return query.ExecWithNolock(q => q.First());
- }
- /// <summary>
- /// Firsts the with nolock asynchronous.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static async Task<T> FirstWithNolockAsync<T>(this IQueryable<T> query)
- {
- return await query.ExecWithNolockAsync(q => q.FirstOrDefaultAsync());
- }
- /// <summary>
- /// Converts to listwithnolock.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static List<T> ToListWithNolock<T>(this IQueryable<T> query)
- {
- return query.ExecWithNolock(q => q.ToList());
- }
- /// <summary>
- /// Converts to listwithnolockasync.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static async Task<List<T>> ToListWithNolockAsync<T>(this IQueryable<T> query)
- {
- return await query.ExecWithNolockAsync(q => q.ToListAsync());
- }
- /// <summary>
- /// Converts to arraywithnolock.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static T[] ToArrayWithNolock<T>(this IQueryable<T> query)
- {
- return query.ExecWithNolock(q => q.ToArray());
- }
- /// <summary>
- /// Converts to arraywithnolockasync.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static async Task<T[]> ToArrayWithNolockAsync<T>(this IQueryable<T> query)
- {
- return await query.ExecWithNolockAsync(q => q.ToArrayAsync());
- }
- /// <summary>
- /// Counts the with nolock.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static int CountWithNolock<T>(this IQueryable<T> query)
- {
- return query.ExecWithNolock(q => q.Count());
- }
- /// <summary>
- /// Counts the with nolock asynchronous.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static async Task<int> CountWithNolockAsync<T>(this IQueryable<T> query)
- {
- return await query.ExecWithNolockAsync(q => q.CountAsync());
- }
- /// <summary>
- /// Longs the count with nolock.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static long LongCountWithNolock<T>(this IQueryable<T> query)
- {
- return query.ExecWithNolock(q => q.LongCount());
- }
- /// <summary>
- /// Longs the count with nolock asynchronous.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="query">The query.</param>
- /// <returns></returns>
- public static async Task<long> LongCountWithNolockAsync<T>(this IQueryable<T> query)
- {
- return await query.ExecWithNolockAsync(q => q.LongCountAsync());
- }
- }
- }
|