In addition to King LinqPad My Extensions :
public static IQueryable<T> DumpNoLock<T>(this IQueryable<T> query) { using (var txn = GetNewReadUncommittedScope()) { return query.Dump(); } } public static System.Transactions.TransactionScope GetNewReadUncommittedScope() { return new System.Transactions.TransactionScope( System.Transactions.TransactionScopeOption.RequiresNew, new System.Transactions.TransactionOptions { IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted }); } public static IQueryable<T> DumpNoLock<T>(this IQueryable<T> query, string description) { using (var txn = GetNewReadUncommittedScope()) { return query.Dump(description); } } public static List<T> ToListNoLock<T>(this IQueryable<T> query) { using (var txn = GetNewReadUncommittedScope()) { return query.ToList(); } } public static U NoLock<T,U>(this IQueryable<T> query, Func<IQueryable<T>,U> expr) { using (var txn = GetNewReadUncommittedScope()) { return expr(query); } }
The latter means that you can do NOLOCK for any evaluative queries for which NOLOCK is not explicitly specified (for example, I got ToListNoLock above). So for example:
somequery.NoLock((x)=>x.Count()).Dump();
will evaluate the request using NOLOCK .
Please note that you must ensure that you are evaluating the request. For example. .NoLock((x)=>x.Distinct()).Count().Dump() will not be anything useful different from .Distinct().Count().Dump() .
Mark Hurd Oct 31 '14 at 5:44 2014-10-31 05:44
source share