所以我在ef core中有一个如下的查询,这会导致重复的结果,甚至很难首先调用im或默认调用它
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中是实体框架)-这通常使用外部应用生成不重复的结果。
编辑:
我的ef核心版本是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; }
}
最佳答案
不幸的是,ef core查询翻译还远远不是最好的。如果您打开ef core日志记录,您将看到许多警告,有些表达式无法翻译,将在本地进行计算(所谓的客户端计算)。
对于这个特定的查询,我建议的唯一解决方法是消除join
和let
语句,并对map_HasPatient
和map_IdEncounter
投影属性使用子查询(具有重复的连接筛选器条件):
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();
它转换为如下SQL查询:
SELECT [r].[IdResult], (
SELECT CASE
WHEN EXISTS (
SELECT 1
FROM [audit_DataEncounters] AS [e]
WHERE ((([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]))
THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
END
) AS [map_HasPatient], (
SELECT TOP(1) [e0].[IdEncounter]
FROM [audit_DataEncounters] AS [e0]
WHERE ((((([r].[IdAudit] = [e0].[IdAudit]) AND (([r].[PatientFirstName] = [e0].[FirstName]) OR ([r].[PatientFirstName] IS NULL AND [e0].[FirstName] IS NULL))) AND (([r].[PatientLastName] = [e0].[LastName]) OR ([r].[PatientLastName] IS NULL AND [e0].[LastName] IS NULL))) AND ([r].[PatientDOB] = [e0].[DateOfBirth])) AND ([e0].[StartDate] <= [r].[TransactionDate])) AND ([r].[TransactionDate] <= COALESCE([e0].[EndDate], @__maxDate_1))
ORDER BY [e0].[Inpatient], [e0].[StartDate] DESC
) AS [map_IdEncounter]
FROM [audit_Results] AS [r]
WHERE (([r].[IdAudit] = @__idAudit_0) AND ([r].[PatientFirstName] = N'ERIC')) AND ([r].[PatientLastName] = N'BROOKS')