|
public static>{ 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();
}
}
private static Regex s_withNoLockRegex =
new Regex(@"(] AS \[t\d+\])", RegexOptions.Compiled);
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 SqlCommand GetCommand(
this DataContext dataContext, IQueryable query, bool withNoLock)
{
SqlCommand command = (SqlCommand)dataContext.GetCommand(query);
if (withNoLock)
{
command.CommandText = AddWithNoLock(command.CommandText);
}
return command;
}
}
|
|
|