LINQ-to-SQL - when does it access the database?

319 views Asked by At

In my asp.net mvc 2 app I have an action which is rather data-heavy. I'm using linq-to-sql for the queries, while using the repository pattern. When I check the execution of the action in SQL Server profiler, I find that approximately 60 queries are being executed. My question is - and I hope there is enough infomation in the question to answer it - does asp.net mvc make a full round trip, back and fourth, to the database, for each and every query, or is it that because of the use of linq, there is actually only one round trip ?

2

There are 2 answers

2
Matt Hudson On BEST ANSWER

It depends on what your Linq looks like. It's my understanding that when your result set enumerates you will hit the database.

3
Chris Hogan On

I believe it's one round trip per query, whether that is a LINQ query or raw SQL. I suppose it depends on what you mean by "each and every query."

To answer your comment above:

var result = LINQ; <--- Query is generated, but not executed

foreach( YourDataType ydt in result ) { <--- Query executes when you do something with result

...

}