我写了一个查询,说它翻译不正确。 查询如下:
var query = from a in context.PlaceSyncs
join b in (
from aa in context.PlaceIdentifiers
join bb in context.PlaceSources on aa.PlaceSourceId equals bb.PlaceSourceId
select new { Id = aa.PlaceIdentifierId, PlaceKey = aa.PlaceIdentifierKey, PlaceSource = bb.PlaceSourceName }
) on new { a.PlaceKey, PlaceSource = a.PlaceSource.ToLower() } equals new { b.PlaceKey, PlaceSource = b.PlaceSource.ToLower() } into temp
from c in temp.DefaultIfEmpty()
where c == null
select new PlaceSync()
{
PlaceActive = a.PlaceActive,
PlaceAddress = a.PlaceAddress,
PlaceInformation = a.PlaceInformation,
PlaceKey = a.PlaceKey,
PlaceName = ((a.PlaceName.Contains("#") == false) ? a.PlaceName : a.PlaceName.Substring(0, a.PlaceName.IndexOf("#"))).TrimEnd().TrimStart(),
PlaceSource = a.PlaceSource,
PlaceSyncId = a.PlaceSyncId
};
var test = query.Count();
当我在linqpad中运行此程序时,所有内容都会检出。 当我在应用程序中运行此命令时,出现以下错误:
The LINQ expression 'DbSet<PlaceSync>
.LeftJoin(
outer: DbSet<PlaceIdentifier>
.Join(
outer: DbSet<PlaceSource>,
inner: p0 => p0.PlaceSourceId,
outerKeySelector: p1 => p1.PlaceSourceId,
innerKeySelector: (p0, p1) => new TransparentIdentifier<PlaceIdentifier, PlaceSource>(
Outer = p0,
Inner = p1
)),
inner: p => new {
PlaceKey = p.PlaceKey,
PlaceSource = p.PlaceSource.ToLower()
},
outerKeySelector: ti => new {
PlaceKey = ti.Outer.PlaceIdentifierKey,
PlaceSource = ti.Inner.PlaceSourceName.ToLower()
},
innerKeySelector: (p, ti) => new TransparentIdentifier<PlaceSync, TransparentIdentifier<PlaceIdentifier, PlaceSource>>(
Outer = p,
Inner = ti
))
.Where(ti0 => new {
Id = ti0.Inner.Outer.PlaceIdentifierId,
PlaceKey = ti0.Inner.Outer.PlaceIdentifierKey,
PlaceSource = ti0.Inner.Inner.PlaceSourceName
} == null)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
我将.netcore与Micrisoft.EntityFrameworkCore版本3.1.5一起使用。 在此版本的ef中是否缺少某些东西,会阻止查询正确翻译?
如果您可以提供在框架中确实有效的同一查询的示例,那么
答案 0 :(得分:0)
EG Core 3.x查询翻译器无法翻译的表达式是反联接条件
where c == null
实际上,这种表达式没有SQL等效项-在SQL中进行左外部反连接时,人们会从右侧使用某些列,该列通常不可为空(通常为PK),以进行IS NULL
检查。
在LINQ查询中可以使用相同的解决方法,但与SQL自然地对任何表达式(甚至非空列)支持NULL的SQL不同,C#表达式将需要显式转换为相应的可空类型(并忽略相关警告)。
例如,如果aa.PlaceIdentifierId
的类型为int
,则条件为
where (int?)c.Id == null
答案 1 :(得分:0)
最后我还是选择了.FromSqlRaw ...