1

我有以下操作方法:-

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);

        }

它调用以下存储库方法:-

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);

        }

但我注意到,如果我将我的操作方法中的 .Tolist() 位置更改为:-

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);

            }

那么将完成对存储库方法的两个查询?谁能解释为什么在同一条语句中使用 .Select 和 .Tolist() 会导致执行两个类似的查询。?

谢谢

4

2 回答 2

0

在您的存储库中,您正在构建您正在利用延迟执行的查询

您的第一个查询触发

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

这个说法

第二个查询

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

您必须用IT360ID替换服务器...

于 2013-11-13T07:25:01.257 回答
0

它在做延迟加载。关闭延迟加载并使用 "include" 代替: ".include("tablename")"

include 在 sql 语句中用作连接

关闭延迟加载:在 Entity Framework 4 中默认禁用延迟加载

于 2013-11-13T00:45:54.797 回答