I have this method that returns the Count of rows with the given search criteria:
public int HistoryCount(Guid id, string tableName)
{
int sqlCount = context.Database.SqlQuery<int>("SELECT COUNT(*) FROM
COMMON.TbHistoryLog WHERE ObjectId = '" + id + "'
AND TableName = '" + tableName + "'").First();
FuncHistory = x => x.ObjectId == id && x.TableName == tableName;
return AuditHelper.HistoryCount(context.TbHistoryLog, FuncHistory);
}
and this is AuditHelper.HistoryCount
method:
public static int HistoryCount<TSet>(DbSet<TSet> set, Func<TSet, bool> predict) where TSet : class
{
var auditCount = set.Count(predict);
return auditCount;
}
I was running into long querying time when executing AuditHelper.HistoryCount method, and then I tried to run the same query (I guess) using raw SQL, which returned the result immediately.
Is there something wrong with my implementation or raw SQL is faster than the equivalent LINQ methods?