Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq到NHibernate在一个请求中生成3000多条SQL语句!_Linq_Nhibernate_Linq To Nhibernate - Fatal编程技术网

Linq到NHibernate在一个请求中生成3000多条SQL语句!

Linq到NHibernate在一个请求中生成3000多条SQL语句!,linq,nhibernate,linq-to-nhibernate,Linq,Nhibernate,Linq To Nhibernate,在过去的几个月里,我一直在使用Linq到NHibernate开发一个Web应用程序,但直到现在还没有分析它生成的SQL。使用NH Profiler,在执行Linq表达式时,下面的代码块似乎命中DB超过3000次 var activeCaseList = from c in UserRepository.GetCasesByProjectManagerID(consultantId) where c.CompletionDa

在过去的几个月里,我一直在使用Linq到NHibernate开发一个Web应用程序,但直到现在还没有分析它生成的SQL。使用NH Profiler,在执行Linq表达式时,下面的代码块似乎命中DB超过3000次

        var activeCaseList = from c in UserRepository.GetCasesByProjectManagerID(consultantId)
                             where c.CompletionDate == null
                             select new { c.PropertyID, c.Reference, c.Property.Address, DaysOld = DateTime.Now.Subtract(c.CreationDate).Days, JobValue = String.Format("£{0:0,0}", c.JobValue), c.CurrentStatus };
其中存储库方法如下所示:

    public IEnumerable<Case> GetCasesByProjectManagerID(int projectManagerId)
    {
        return from c in Session.Linq<Case>()
               where c.ProjectManagerID == projectManagerId
               select c;
    }
它似乎首先运行初始存储库查询,然后遍历所有结果,检查CompletionDate是否为null,但首先发出查询以获取c.Property.Address

因此,如果初始查询返回2000条记录,即使其中只有5条没有CompletionDate,它仍然会触发SQL查询以返回2000条记录的地址详细信息

我想象这会起作用的方式是,它将评估所有WHERE和SELECT子句,并简单地合并它们,因此初始查询如下所示:

选择。。。其中ProjectManager=@p1和CompleteDate不为空

这将产生5条记录,然后它可以激发进一步的5个查询来获得地址。我是期望过高,还是我只是做错了什么


Anthony

更改GetCasesByProjectManagerID的声明:

public IQueryable<Case> GetCasesByProjectManagerID(int projectManagerId)

你不能用IEnumerable组合查询——它们只是序列。IQueryable是专门为这样的构图而设计的。

因为我还不能添加注释。Jon Skeet是对的,您需要使用IQueryable,这允许Linq提供程序惰性地构造SQL。IEnumerable是渴望的版本