Iteration over a linq to sql query is very slow?

GetAdvertForCustomerID doesn't return results, it returns a query ToList enumerates the query. The results are hydrated at that point. The database trip is happening within the ToList call You should get the generated sql (by using the SqlProfiler or the DataContext.

Log property), and take it to query analyzer to examine the execution plan and IO (SET STATISTICS IO ON).

GetAdvertForCustomerID doesn't return results, it returns a query. ToList enumerates the query. The results are hydrated at that point.

The database trip is happening within the ToList call. You should get the generated sql (by using the SqlProfiler or the DataContext. Log property), and take it to query analyzer to examine the execution plan and IO (SET STATISTICS IO ON).

David is very right here. IMO you should implicitly return IQueryables. Either return a List or T or use IQueryable as return type of the method definition.

That makes it very explicit what will be happening. – Steven Jun 16 '10 at 11:47 I don't have any beef with the return type of the method. A basic linq fact is that queries are executed when enumerated.

ToList enumerates its source. This knowledge is as essential in the linq world as knowing that lines of code are executed in order. – David B Jun 16 '10 at 11:55 Typo in my previous comment.It should read "you should NOT implicitly return IQueryables".

– Steven Jun 16 '10 at 12:09 However, users of your method might not expect that iterating the collection twice will result in two calls to the database. – Steven Jun 16 '10 at 12:09 If I was given an IQueryable and had no knowledge of what generated it, I would certainly treat that as an expensive iterate-once-only source. – David B Jun 16 '10 at 12:21.

. ToList will execute the query, so that includes the entire roundtrip to the database, getting the data, and materializing the entity objects for every record returned... ...you're most likely spending most of that time db-side, so take a closer look at the execution plan for your view.

DB side execution time (according to SqlServer profiler = 0, with 139 IO ops. – devzero Jun 16 '10 at 13:16.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions