【问题标题】:Prevent the EF from making two queries防止 EF 进行两次查询
【发布时间】:2019-12-06 16:51:23
【问题描述】:

是否可以阻止 EF 在下面的查询中进行两次查询?

var regitriesNames = 
                from registryView in registryViewRepository.GetAll()
                where (
                    from registryReport in registryReportRepository.GetAll()
                    where registryReport.ReportId == reportId
                    select registryReport.RegistryViewId
                ).Contains(registryView.Id)
                select registryView.Name;

查询工作正常。我唯一想避免的是重复使用GetAll()

那么,是否有可能以某种方式改进查询?

我有两个实体:RegistryViewReport,我有一个实体代表它们之间的多对多关系 RegistryReport。 p>

【问题讨论】:

  • 如果可能,存储 GetAll 方法会导致一个 variable 通过查询。
  • 为什么要调用 GetAll()? registryViewRepository 和 registryReportRepository 是否共享一个 DbContext 实例?

标签: sql entity-framework orm


【解决方案1】:

请检查这个。

代码

var lstdata = registryViewRepository.GetAll();
var regitriesNames = from registryView in lstdata 
                where (
                    from registryReport in lstdata
                    where registryReport.ReportId == reportId
                    select registryReport.RegistryViewId
                ).Contains(registryView.Id)
                select registryView.Name;

【讨论】:

  • 很高兴这有帮助,谢谢:)
猜你喜欢
  • 1970-01-01
  • 2015-07-12
  • 2014-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多