我写了一个查询,说它没有正确翻译。查询如下:
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.
我正在与Micrisoft一起使用.netcore。EntityFrameworkCore 3.1.5版。这个版本的ef中是否缺少一些东西,会妨碍查询的正确翻译?
如果你能提供一个在框架中工作的相同查询的例子,那就太好了。
EG Core 3.x查询翻译器无法翻译的表达式是反联接条件
where c == null
事实上,这样的表达式并没有SQL等价物——在SQL中执行左外反联接时,会使用右侧的某个列(通常是PK(进行IS NULL
检查。
这也可以用作LINQ查询中的变通方法,但与SQL不同,在SQL中,任何表达式(即使是不可为null的列(都自然支持NULL,C#表达式将需要显式转换为相应的可为null类型(并忽略相关警告(。
例如,如果aa.PlaceIdentifierId
的类型是int
,那么标准将是
where (int?)c.Id == null
最后我还是选择了。从SqlRaw。。。