1

I have the following Action method:-

public ActionResult RackServer(int id) {
            var servers = repository.AllFindRack_Servers(id).ToList();
            var IT360IDs = servers.Select(a => a.Technology.IT360ID.Value).ToArray();

            return PartialView("~/Views/Server/_nonPagedServerTable.cshtml", servers);

        }

which calls the following repository method:-

public IQueryable<TMSServer> AllFindRack_Servers(int id)
        {



            return tms.TMSServers.Where(c => c.RackID == id)
            .Include(a => a.OperatingSystem)

            .Include(a3 => a3.TMSRack.DataCenter)
            .Include(a14 => a14.TMSRack.Technology)

            .Include(a6 => a6.TMSVirtualMachines)
            .Include(a7 => a7.TechnologyStatu)
            .Include(a8 => a8.TechnologyBackUpStatu)
            .Include(a9 => a9.TechnologyRole)
            .Include(a10 => a10.Technology.TechnologyIPs)
            .Include(a11 => a11.TMSServer1.Technology)
            .Include(a14=>a14.TMSServers1)
            .Include(a12 => a12.TMSSwitches)
            .Include(a13 => a13.TMSFirewalls);

        }

but i have noted that in case i chnage the .Tolist() position within my action method to be as:-

public ActionResult RackServer(int id) {
                var servers = repository.AllFindRack_Servers(id);
                var IT360IDs = servers.Select(a => a.Technology.IT360ID.Value).Tolist().ToArray();

                return PartialView("~/Views/Server/_nonPagedServerTable.cshtml", servers);

            }

then two queries to the repository method will be done ? can anyone explain why having .Select & .Tolist() inside the same statement is causing two similar quires to be executed.?

Thanks

John John
  • 1
  • 72
  • 238
  • 501

2 Answers2

0

its doing lazy loading. turn lazy loading off and use "include" instead: ".include("tablename")"

include works as join in sql statements

turning lazyloading off: Disable lazy loading by default in Entity Framework 4

Community
  • 1
  • 1
JC Lizard
  • 1,050
  • 6
  • 17
0

in your repository you are building your query where you are taking advantage of deferred execution

your first query fires on

 var IT360IDs = servers.Select(a => a.Technology.IT360ID.Value).Tolist().ToArray();

this statement

second query on

 return PartialView("~/Views/Server/_nonPagedServerTable.cshtml", servers); 
 //as here you are accessing servers object

you have to replace servers with IT360IDs...

Vishal Sharma
  • 2,773
  • 2
  • 24
  • 36