I am trying to get a total record count from the below Method using EntityFramework and Linq. It is slow to return a count.
public static int totalTracking(int id)
{
using (var ctx = new GPEntities())
{
var tr = ctx.Tracking
.Where(c => c.clientID == Config.ClientID)
.Where(c => c.custID == id)
.Where(c => c.oOrderNum.HasValue)
.ToList();
return tr.Count();
}
}
You can simplify the query significantly:
When you call
ToListthis will trigger materialization, so query will be issued to the database and all the columns will be retrieved. The actual count would happen on the client.If you do just
Count, withoutToListit will issue query when you callCountand server will return just one number, instead of a table.This is not that critical for performance, but I think the code would look a little nice without that many
Where:or even