views:

210

answers:

2

Hi,

I have the following pretty simple linq query querying a linq to entities edmx.

(from i in ent.Inspectors select i).OrderBy(s => s.Surname).Skip((page - 1) * count).Take(count).ToList();

In Sql Server Profiler I can see that the exact same select query is being sent twice.

Can someone explain why?

Cheers,

Dave

+1  A: 

Is ent.Inspectors an IEnumerable containing two items?

DamienG
A: 

Because of deffered execution, the results of the query aren't cached locally. To prevent this, add a call to ToArray in the query.

Also, from i in ent.Inspectors select i is a no-op; you should write ent.Inspectors.OrderBy(s => s.Surname)....

SLaks
He already has a ToList().
Craig Stuntz
Yes, but that's at the end. If one of the methods before it doesn't get translated by L2E, it can still hit twice.
SLaks
All of those methods are known by L2E. Unless there's something **significant** he's not telling us, that should only be one query.
Craig Stuntz
@Craig: Yes, you have a point. I'm not sure.
SLaks
Thanks for the replies. @Slacks, good point about replacing the from i in ... If the query was to remain getting everything then I would, but it's just the starting point at the moment. :D
DavidGouge