所以我在ef核心中有一个以下查询,这导致重复的结果甚至很难我首先调用或默认它
DateTime maxDate = SqlDateTime.MaxValue.Value;
var results = (from r in _dbContext.audit_Results
join e in _dbContext.audit_DataEncounters on new { r.IdAudit, r.PatientFirstName, r.PatientLastName, r.PatientDOB } equals new { e.IdAudit, PatientFirstName = e.FirstName, PatientLastName = e.LastName, PatientDOB = e.DateOfBirth } into eJoin
let e = eJoin.Where(it => it.StartDate <= r.TransactionDate && r.TransactionDate <= (it.EndDate ?? maxDate))
.OrderBy(it => it.Inpatient).OrderByDescending(it => it.StartDate)
.FirstOrDefault()
where r.IdAudit == idAudit && r.PatientFirstName == "ERIC" && r.PatientLastName == "BROOKS"
select new AuditResultBulkModel()
{
IdResult = r.IdResult,
map_HasPatient = eJoin.Any(),
map_IdEncounter = e != null ? e.IdEncounter : (int?)null
}).ToList();
生成的查询如下所示:
SELECT *
FROM [audit_Results] AS [r]
LEFT JOIN [audit_DataEncounters] AS [e] ON ((([r].[IdAudit] = [e].[IdAudit]) AND (([r].[PatientFirstName] = [e].[FirstName]) OR ([r].[PatientFirstName] IS NULL AND [e].[FirstName] IS NULL))) AND (([r].[PatientLastName] = [e].[LastName]) OR ([r].[PatientLastName] IS NULL AND [e].[LastName] IS NULL))) AND (([r].[PatientDOB] = [e].[DateOfBirth]) OR ([r].[PatientDOB] IS NULL AND [e].[DateOfBirth] IS NULL))
WHERE (([r].[IdAudit] = @__idAudit_1)
ORDER BY [r].[IdAudit], [r].[PatientFirstName], [r].[PatientLastName], [r].[PatientDOB]
有人可以告诉我如何从中得到正确的结果吗?
过去(.net Framework中的实体框架) - 这用于使用OUTER APPLY生成非重复结果。
编辑: 我的EF Core版本是2.0.0。
数据模型如下:
public class audit_Result
{
[Key]
public int IdResult { get; set; }
public int IdAudit { get; set; }
public bool? map_HasPatient { get; set; }
public int? map_IdEncounter { get; set; }
public virtual audit_Audit audit_Audit { get; set; }
}
public class audit_DataEncounter: IAuditEntity
{
[Key]
public int IdEncounter { get; set; }
public int IdAudit { get; set; }
[StringLength(50)]
public string FirstName { get; set; }
[StringLength(50)]
public string LastName { get; set; }
public DateTime? DateOfBirth { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public bool? Inpatient { get; set; }
public virtual audit_Audit Audit { get; set; }
}
public class audit_Audit
{
[Key]
public int IdAudit { get; set; }
public virtual ICollection<audit_DataEncounter> audit_DataEncounters { get; set; }
public virtual ICollection<audit_Result> audit_Result { get; set; }
}
答案 0 :(得分:5)
不幸的是,EF Core查询翻译仍然是(截至目前为止的最新版本2.0)远远不是最好的。如果您打开EF Core日志记录,您将看到许多警告,表明某些表达式无法翻译并将在本地进行评估(所谓的客户评估)。
我可以为此特定查询建议的唯一解决方法是消除join
和let
语句并使用map_HasPatient
和{{1}的子查询(具有重复的连接过滤条件)投影属性:
map_IdEncounter
转换为SQL查询,如下所示:
var results = (
from r in _dbContext.audit_Results
where r.IdAudit == idAudit && r.PatientFirstName == "ERIC" && r.PatientLastName == "BROOKS"
select new AuditResultBulkModel
{
IdResult = r.IdResult,
map_HasPatient = _dbContext.audit_DataEncounters
.Any(e => r.IdAudit == e.IdAudit && r.PatientFirstName == e.FirstName && r.PatientLastName == e.LastName && r.PatientDOB == e.DateOfBirth),
map_IdEncounter = _dbContext.audit_DataEncounters
.Where(e => r.IdAudit == e.IdAudit && r.PatientFirstName == e.FirstName && r.PatientLastName == e.LastName && r.PatientDOB == e.DateOfBirth
&& e.StartDate <= r.TransactionDate && r.TransactionDate <= (e.EndDate ?? maxDate))
.OrderBy(e => e.Inpatient).ThenByDescending(e => e.StartDate)
.Select(e => (int?)e.IdEncounter)
.FirstOrDefault()
}).ToList();