【发布时间】:2020-04-09 07:08:31
【问题描述】:
好的,所以我有以下 linq to sql 查询:
var baseQuery = _context.AuditTransaction
.Include(at => at.AuditEntityEntries)
.ThenInclude(aee => aee.AuditPropertyEntries)
.Where(at => auditTransactionFilter.Id == 0 || at.Id == auditTransactionFilter.Id)
.Where(at =>
string.IsNullOrWhiteSpace(auditTransactionFilter.UserName) ||
at.UserName == auditTransactionFilter.UserName);
return baseQuery
.Where(at => at.AuditEntityEntries.Any() || at.AuditEntityEntries
.Any(ataee => auditTransactionFilter.AuditEntityEntries
.Any(atfaee =>
(atfaee.Id == 0 || ataee.Id == atfaee.Id) &&
(string.IsNullOrWhiteSpace(atfaee.TableName) ||
ataee.TableName.ToLower().Contains(atfaee.TableName.ToLower()))
&&
(string.IsNullOrWhiteSpace(atfaee.KeyValues) ||
ataee.KeyValues.ToLower().Contains(atfaee.KeyValues.ToLower()))
&& (atfaee.AuditPropertyEntries.Any() || ataee.AuditPropertyEntries
.Any(atape => atfaee.AuditPropertyEntries
.Any(atfape =>
(atfape.Id == 0 || atape.Id == atfape.Id) &&
(string.IsNullOrWhiteSpace(atfape.PropertyName) ||
atape.PropertyName == atfape.PropertyName)
&&
(string.IsNullOrWhiteSpace(atfape.AuditType.ToString()) ||
atape.AuditType == atfape.AuditType)
&&
(atfape.PropertyValues == null ||
atape.PropertyValues.Contains(atfape.PropertyValues))
))
)
)
));
无论出于何种原因,当它运行时,我收到数百条警告,它们的各个部分无法翻译,将在本地进行评估。但是,查询运行并没有正确过滤掉不匹配项:返回数据库中的所有记录。
但是,当我返回 baseQuery.ToList().Where(...) 时,查询运行完美。
更新:即使是简单的查询,例如:
return baseQuery
.Where(at => at.AuditEntityEntries.Any(ataee => auditTransactionFilter.AuditEntityEntries.Any(atfaee => atfaee.TableName == ataee.TableName)));
返回如下错误:
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'where ([atfaee].TableName == [ataee].TableName)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Any()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'where {from AuditEntityEntry atfaee in __auditTransactionFilter_AuditEntityEntries_0 where ([atfaee].TableName == [ataee].TableName) select [atfaee] => Any()}' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'where ([atfaee].TableName == [ataee].TableName)' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'Any()' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
The LINQ expression 'where ?= (Property([at], "Id") == Property([ataee], "AuditTransactionId")) =?' could not be translated and will be evaluated locally.
warn: Microsoft.EntityFrameworkCore.Query[20500]
我在这里做错了什么?
请帮忙。
【问题讨论】:
-
“当它运行时,我收到数百个警告”您在运行时收到警告?
-
此外,
return语句中的Where子句可以简化为第一个条件,因为||的第二部分是第一个的过滤版本(如果第一个是true,那么第二个永远是true)。您可以将其简化为return baseQuery.Where(at => at.AuditEntityEntries.Any()。 -
@Rufus,在运行时查询执行时会出现警告。谢谢,我已将所有多余的空安全检查修改为 .Any()。我仍然收到警告。
-
另外,我在嵌套迭代中添加了一个简单的尝试,但它失败了。我更新了问题以反映。
标签: c# sql linq .net-core linq-to-sql