Saturday 16 February 2008 @ 8:29 am
Recently, I recieved the following question on my blog regarding a post I made about using the aggregate function Count. The question is as follows:\
does the LINQ implementation send a full query or just a count query? That can be a big waste, especially if this is called often?
One of the nice things about LINQ is you don’t have to guess. By simply turning the Log property to true of the DBContext, you can see what LINQ is actually sending to the database. below is some simple code that shows how to do this.
protected void Page_Load(object sender, EventArgs e) { DataClassesDataContext db = new DataClassesDataContext(); StringWriter sw = new StringWriter(); db.Log = sw; int cnt = (from tbl in db.tblAllTimes select tbl).Count(); TextBoxResults.Text = sw.ToString(); }
The results of running this are as follows:
So, the answer is that is sending what seems to me to be the most efficient request it can.
I more mystery solved.
Thanks for reading.











April 2nd, 2008 at 5:17 pm
It does most of the time.
April 3rd, 2008 at 3:21 am
Atleast in this i would differ from your answer as in teh select the query is mentioend as select count(*) from [value] but if it is count(1) it makes the most effective that with (*).