user560498
user560498

Reputation: 547

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

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 ?

Upvotes: 1

Views: 319

Answers (2)

Chris Hogan
Chris Hogan

Reputation: 868

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

...

}

Upvotes: 2

Matt Hudson
Matt Hudson

Reputation: 7348

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

Upvotes: 1

Related Questions