0

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

DavidGouge
  • 4,583
  • 6
  • 34
  • 46
  • Can you verify that the line of code you're describing isn't being hit twice? – Joseph Oct 08 '09 at 15:39
  • Are you sure it's *actually* being sent twice? SQL Profiler, by default, will show the same query more than once. Can you show the trace? – Craig Stuntz Oct 08 '09 at 15:40
  • 2
    @Craig. Bingo! Thanks for the reply. Giving the trace more than a cursory man-glance I could see that one was a BatchStarting and the other was a BatchCompleted. . – DavidGouge Oct 12 '09 at 13:39

2 Answers2

1

Is ent.Inspectors an IEnumerable containing two items?

DamienG
  • 6,575
  • 27
  • 43
0

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
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 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 Oct 08 '09 at 18:37
  • 1
    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 Oct 08 '09 at 21:38
  • 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 Oct 12 '09 at 13:41